Good morning all
I have been playing around with setting up a file upload function on my HTML form and just cant get the file to actually upload onto my server. The directory is named /uploads and is in my root where the index file etc is housed. The form has the right enctype etc and the PHP does not return any errors, i just cant get the file to upload. Please help me ....
The HTML for my input field is :
<td valign="top">File Upload</label></td>
<td valign="top"><input name="file" type="file" /></td>
PHP is :
$uploads_dir = "http://www.tenancydepositservice.co.uk/uploads";
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) { $tmp_name = $_FILES["file"]["name"]; $name = $_FILES["file"]["name"]; move_uploaded_file($tmp_name, "$uploads_dir/$name");}
$email_message .= "$uploads_dir/$name";
You have wrong temp name it should be
$tmp_name = $_FILES["file"]["tmp_name"]
you had
$tmp_name = $_FILES["file"]["name"];
also make sure the form has
enctype="multipart/form-data"
And the file save path is as
$uploads_dir = "path to upload"
something as
$uploads_dir = "/var/www/project/upload_dir"
having upload_dir
with write permission.
I hope this works:
the php part / file:
<?php
if($_POST) {
$uploads_dir = './uploads';
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$allowed_type = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'); //this is for images but can be changed to any type of file
$file_tmp = $_FILES['file']['tmp_name'];
$path = $uploads_dir.$file_name;
if(isset($file_name) && !empty($file_name)) {
if(in_array($file_type, $allowed_type)) {
move_uploaded_file($file_tmp, $path);
//if you need to store the file in a db use $path in your query
}
}
}
?>
Copied from w3schools.com
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
$allowedExts = array("gif", "jpeg", "jpg", "png"); //add your extension here
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg") //add extension here as well
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) //create a folder
{ //named upload where your
echo $_FILES["file"]["name"] . " already exists. "; //all php pages are stored
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
look at // in code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.