i'm going to create a gallery website and it will contain a lot of
alboums and photos, videos. now what the right thing: 1-insert the photos and videos in database (if it right tell me how) 2-make an html page for each alboum. thanks in advance.
Configure The "php.ini" File
First, ensure that PHP is configured to allow file uploads.
In your "php.ini" file, search for the file_uploads directive, and set it to On:
file_uploads = On
Create The HTML Form
Next, create an HTML form that allow users to choose the image file they want to upload:
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Some rules to follow for the HTML form above:
Make sure that the form uses method="post". The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form
Without the requirements above, the file upload will not work.
Other things to notice:
The type="file" attribute of the <input> tag shows the input field as a file-select control, with a "Browse" button next to the input control
The form above sends data to a file called "upload.php", which we will create next.
Create The Upload File PHP Script
The "upload.php" file contains the code for uploading a file:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
more info :
https://www.w3schools.com/php/php_file_upload.asp
https://sites.google.com/site/prgimr/how-to-upload-image-or-files-into-database-using-php-and-mysql
https://www.youtube.com/watch?v=3OUTgnaezNY
Third option probably is the best. Store them in a cloud storage like AWS S3, Azure Blob Storage or Google Cloud Storage and many other options. They are much cheaper than a database and more versatile then storing as html.
Related
I have two PHP files.
index.php allows users to upload an image. The uploaded data is sent to another page i.e. upload.php, where I check certain conditions for example if the uploaded image is really an image and not some other file and if the image already exists on the server etc;
I want to see that my upload.php page redirects back to the index.php page in false conditions. I have seen many of the posts on redirecting, e.g., by using header, which I did. Well, It does redirect. However, there is some part of javascript code, which does not execute. I want to see that alert box on my upload.php page, which upon user interaction redirects to index.php.
Alert box does not appear at the moment, but the page does redirect
My question might be very trivial, but I am a PHP newbie.
index.php
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
upload.php
$target_dir = "uploads/";
$file_name=basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir . $file_name;
$uploadOk = 1;
if (file_exists($target_file)) {
$uploadOk = 0;
echo "<script language='javascript'>
alert('Sorry, file already exists.Consider Renaming or upload new file');
</script>";
header("Location: http://xx.yyy.zz");
exit;
}
You can replace the 'HTTP header redirect' with a redirection done in javascript.
Remove the code
header("Location: http://xx.yyy.zz");
And replace the echo command with
echo "<script language='javascript'>
alert('Sorry, file already exists.Consider Renaming or upload new file');
window.location = "http://xx.yyy.zz";
</script>";
I have uploaded a file in my database below row named 'file_name'. Now when I open my edit form in view mode, the file name does not show (is empty) and when I save the form, the file_name goes blank from the database. I want to save the file in edit form so each time I save the form, the already uploaded file should show in database. I know that value cannot be used for files for security reasons and I also know how to just echo the name of file, but I need to keep the already uploaded file in edit form.
This is my code in edit form in view mode:
<div class="form-group">
<label for="example-text"><?php echo get_phrase('Browse File');?></label>
<input type="file" name="file_name" id = "file_name" class="form-control-file"/><span name="old" id = "old"><?php echo $row['file_name'];?></span>
</div>
This is my code in model:
$page_data['file_name'] = $_FILES["file_name"]["name"];
$oldfile = $_POST['old'];
$file = $_FILES["file_name"]["name"];
if($file != "") {
move_uploaded_file($_FILES["file_name"]["tmp_name"], "uploads/parents_image/" . $_FILES["file_name"]["name"]);
} else {
$file = $oldfile;
$page_data['file_name'] = $this->input->post('file_name');
}
Any advice would be very helpful.
Many thanks. enter image description here
Add
if(isset($_FILES['file_name']) && !empty($_FILES['file_name']){
// your code here
}
I'm trying to get a simple upload system going using php and XMLHttpRequest. However I fail to achieve the goal using the request. It doesn't work..
Here is my HTML form which is used to get the image file:
<form action="../session/uploader.php" method="post" enctype="multipart/form-data" id="uploadForm">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
and this is my PHP script which saves the image into my filesystem:
<?php
if(isset($_POST["submit"])) {
$target_dir = "../db/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>
Now this works perfectly fine but I want to use an XMLHttpRequest so that the page doesn't need to refresh when images are uploaded. I was trying to do something which seemed very straight forward but it didn't work..
first I changed the PHP to this:
<?php
$target_dir = "../db/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
?>
and right below my form in the HTML I've added some Javascript which makes the XMLHttpRequest and sends it:
<script>
const uploadForm = document.getElementById("uploadForm");
uploadForm.addEventListener("submit", function(e) {
e.preventDefault();
const xhr = new XMLHttpRequest();
xhr.open("POST", "../session/uploader.php", true);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(new FormData(uploadForm));
});
</script>
The problem here is that it simply doesn't work. Nothing happens. I get no feedback. It's like the uploader.php script didn't get called.
Doesn anyone know what the problem might be?
if(isset($_POST["submit"]))
The submit button will not be included in the form submission data set here, as it would be if you had just send the form “normally”.
xhr.send(new FormData(uploadForm));
The browser has no idea here at this point, that you clicked the submit button to actually trigger the whole process - so it will only include the data for the fileToUpload file upload field, but not the submit button.
You can either add a hidden field, and then check if that is set within $_POST, or you check if fileToUpload is set directly (but remember, that one is going to be in $_FILES, not $_POST.)
xhr.setRequestHeader("Content-Type", "multipart/form-data");
A proper multipart request also needs to include the boundary that will be used to separate the parts inside the request body, in this header.
Using FormData, this should automatically get added, so remove this line - it is overwriting the correct header with one that is incomplete here at this point.
I investigate the problem of file upload using html5, and I have theoretical question: has html5 any limits for file size for uploading?
For example, can I upload files with size ~500Gb?
P.S.: I use FileReader api for reading file.
Ok. This problem is resolved.
But as it explains in FileReader API:
This interface provides methods to read File objects or Blob objects into memory...
As I understood correctly, I can't read file with size which is more than available RAM?
Nope, there is no size upload limit.
here is the spec and here is a related question on how to check the file size, so that you can add limits if you like.
It's worth pointing out that if you are looking to store the file on the server, you might hit file upload limits/restrictions there. But you should be able to configure them. i.e. php/wordpress default upload limit
Hope this useful..
Form,script for validating:
<form action="check.php" method="post" enctype="multipart/form-data">
<label>Upload An Image</label>
<input type="file" id="file_upload" name="file_upload" />
<input type="submit" onClick="return validate()" name="upload"/>
</form>
<script>
function validate(){
var size=2097152;
var file_size=document.getElementById('file_upload').files[0].size;
if(file_size>=size){
alert('File too large');
return false;
}
var type='image/jpeg';
var file_type=document.getElementById('file_upload').files[0].type;
if(file_type!=type){
alert('Format not supported,Only .jpeg images are accepted');
return false;
}
}
php for uploading:
<?php
if(isset($_POST['upload'])){
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file_upload"]["name"]);
if(move_uploaded_file($_FILES["file_upload"]["tmp_name"], $target_file)){
echo "The file ". basename( $_FILES["file_upload"]["name"]). " has been uploaded.";
}
else{
echo "sorry";
}
}
?>
Getting an issue with the upload image part from the client.
We have a php form called entry where the client will enter some information that we needs to upload an image. After submitting, the information will be saved into an xml file called data.xml and it will be shown on an html file called display.html
The image have to be saved into a folder called upload. We have this code but I think somewhere we are doing it wrong because it's not working.
This is the part for the image:
PHP Code:
$_FILES['file'];
$_FILES["file"]["name"];
$_FILES["file"]["type"];
$_FILES["file"]["size"];
$_FILES["file"]["tmp_name"];
$_FILES["file"]["error"];
if(isset($_POST["file"]['submit'])) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "<br>Stored in: " . "upload/" . $_FILES["file"]["name"];
}
} echo $doc->save($file); } ?>
And in the body html we have this:
<label for="file">Image:</label>
<input type="file" name="file" id="file" action:"entry.php" method:"post" entype:"multipart/form-data"><br>
And also it doesn't save anything in the data.xml file. If I remove this code and leave it as it is, the information are saved in the xml form and the display is working.
Can anyone help please?
Thank you
firstly you need to use form and input type submit.
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
also You have a spelling mistake in enctype:"multipart/form-data" you missed c..
Important parts of the code are missing.
Assuming that you've done everything else correctly, and your problem is that it doesn't save anything in the xml file,you should add the below code just after your second echo:
//foreach takes keys and values from all file input types
foreach($_FILES as $item => $val){
$val=$_FILES["$item"]["name"]; //save each file's name to $val
$fileNode=$doc->createElement($item, $val); //create a new file element(file is an image in your case)
$entry->appendChild($fileNode); //add the file element as a child of another element - $entry must be initialized from before
}