I need to create a simple web page via which you can upload a image file that goes to a directory on the server that is previously created.
Here's the code for the index.php file:
<!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>
Here's the code for the upload.php:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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;
}
}
?>
The code is hosted on a free hosting server (not a paid one) and I created a dir called 'uploads' in the same subdirectory where both scripts are located.
In the php settings uploading seems to be on.
The index.php displays fine, I select an image and click upload, it loads for a second and then displays 'File is an image - image/gif.'
However when I got to the upload dir, there isn't a single file there.
What could be the issue? Thank you in advance.
You aren't moving the file to your server after uploading the temp file so it gets deleted
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
Related
I want to upload an image to a file using php. I keep simplifying the code but it still doesn't work. I'll add back parameters once I figure out what's wrong. Here's my file upload.php
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="upload" id="upload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST["submit"])) {
$pathway= "uploads/";
$target_file = $pathway . basename($_FILES["upload"]["name"]);
if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["upload"]["name"]). " has been uploaded.";
}
}
?>
I've created the folder "uploads", but images are not added to it.
first you have to make sure that the attribute file_uploads = on in your php.ini
file
second make sure to set the upload_max_filesize to the size you want
then see this code
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
I am trying to upload an image via a PHP script.
In the script all conditions are true while I upload the image but move_uploaded_file function doesn't seem to work.
What may be the problem?
My HTML code is
<form id="register_form" action="" 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>
PHP Script:
<?php
$target_dir = "/home/infibusiness1/Desktop/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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;
}
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'],
$target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has
been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
echo "<br>File Name : ".$_FILES['fileToUpload']['tmp_name'];
}
}
?>
Ensure that the web server has write permission on the target directory?
You will need to know what user the web server is running as or if security isn't a concern try
chmod 777 /home/infibusiness1/Desktop/uploads/
I have just setup a fileupload.php so that I can upload files but am interesting in having the files instantly be setup on the page that I want it with the css that I assign. Is there a way to do this? I would like to have three columns of photos that viewers can see as I upload the files so I do not have to manually add it into the code every single time I want to add a photo to the page.
I already know how to make a grid with the photos but am really interested in how to actually have all the photos uploaded to the page to instantly be placed on that grid.
My graphics.php page :
<?php include 'includes/header.php' ?>
<div class="graphics_content">
<div class="page_top_image">
<img src="includes/img/heartfx_graphics.png" class="img-responsive" alt="Responsive image">
</div>
</div>
<div class="photo_upload">
<div class="photo_upload_form">
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<br>
<input type="file" name="fileToUpload" id="fileToUpload">
<br>
<input type="submit" value="Upload Image" name="submit">
</form>
</div>
</div>
My upload.php page :
<?php
$target_dir = "uploads/";
$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]);
$uploadOK = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//Check if image file is an actual image or a 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;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOK = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOK = 0;
}
// Allow certain file formats
if($imageFileType != "png") {
echo "Sorry, only PNG files are allowed.";
$uploadOK = 0;
}
// Check if $uploadOK is set to 0 by an error
if ($uploadOK == 0) {
echo "Sorry, your file was not uploaded.";
// If everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Thank you in advance for any help. If anything is needed from my code that you would like me to provide I can do so.
Best regards,
Codi
I have a form on a webpage that allows users to upload mp3 files. It works perfectly fine except when it comes to files over 2MB. Then it says "Sorry, there was an error uploading your file." I have tested mp3s ranging from 2mb - 6mb and they dont work and I believe I have adjusted the code to where it can accept files well over 2MB. Can someone help?
Here's the HTML, (I don't believe this to be the issue):
<div id="contactpara">
<form id="upform" 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 Music" name="submit">
<iframe id="upload_target" name="upload_target" src="" style="width:500px;height:100px;border:0px solid #000;"></iframe>
</form>
</div>
Here's the PHP:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 10000000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "mp3" ) {
echo "Sorry, only mp3 files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
The reason it says $imageFileType is because this was a PHP code used to accept images that I tweaked to accept audio files. Could this be causing the issue? Please let me know.
I kind of had this same issue and found out it was the max_filesize which was 2m. it makes me think alot cus i troubleshooted.. solution is just to change the max_filesize to the amount you want
I'm new to HTML/PHP and I'm trying to create a simple php file upload page.
I have this as my HTML
<!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>
I have this as my php:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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;
}
}
?>
I've uploaded both of these to the correct folder in my host (000webhost), yet when I check in my /uploads folder nothing is there. I've granted all my files read write and execute permissions to try and debug it - I'll learn about security later.
Any help would be greatly appreciated!
You must add this snippet of code:
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
after that:
echo "File is an image - " . $check["mime"] . ".";
You need to move the uploaded file from the temp directory it was uploaded into, into your target directory. See the PHP docs of move_uploaded_files
With the PHP function move_uploaded_file(string $filename, string $destination) you can move the file to your desired path.