This is my first time attempting to upload a file via PHP.
Here is my HTML:
<form role="form" action="api/upload.php" id="uploadForm" method="post" enctype="multipart/form-data">
<input id="fileToUpload" name="fileToUpload" type="file" class="file" />
<button type="submit" name="submit" id="submit">Upload</button>
</form>
Now here is the PHP script in reference "api/upload.php":
<?php
$target_dir = "files\\";
if(isset($_POST["submit"])) {
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo $uploadOk . "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else{
echo "Sorry, there was an error uploading your file.";
}
}
?>
This may be a logic error. I'm not sure. Regardless, I keep getting the message:
Sorry, there was an error uploading your file
When I echo out $uploadOk, it remains as 1. How can I find my error?
Use $_FILES["fileToUpload"]["tmp_name"] instead of $_FILES["fileToUpload"]["name"]
Should be
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
Notes:-
$_FILES["fileToUpload"]["name"] is just the name of the uploaded file. $_FILES["fileToUpload"]["tmp_name"] is the temporary file that holds the content.
Hope this helps.
[Edit 1]
I was wrong about adding a value="submit" attribute to the button. name="submit" attribute is sufficient for the isset($_POST["submit")) check.
Related
I have a question about file upload. i want to print the original filepath as it was on the users PC (eg 'C:\Documents and settings\guest\documents\test.txt')
i have try some method but only get filename.. i need to print full path
<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
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$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.";
echo "C:\Documents and settings\guest\documents\test.txt"; //get full path
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Sending an upload to my site that is currently just run on localhost. Attempting to upload an .xlsx file gives me my predefined error of "Sorry, you cannot upload this file. Sorry, your file was not uploaded." Code follows:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$uploadFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($uploadFileType != "xlsx") {
echo "Sorry, you cannot upload this file.";
$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.";
}
}
?>
Form:
<form id="uploadbanner" enctype="multipart/form-data" method="post" action="uploads.php">
<input id="fileupload" name="myfile" type="file" />
<input type="submit" value="Submit" id="submit" />
</form>
Renaming your input file to name="fileToUpload" will solve your issue.
<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>
Try this:
$target_file = $_FILES["myfile"]["name"];
$uploadOk = 1;
$uploadFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check file size
if ($_FILES["myfile"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($uploadFileType != "xlsx") {
echo "Sorry, you cannot upload this file.";
$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["myfile"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["myfile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
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