I copied the code from this page: http://www.w3schools.com/php/php_file_upload.asp
Problem is that i don't get an error when uploading a file, instead this shows which should be correct:
Upload: images.jpeg Type: image/jpeg Size: 5.8603515625 kB Temp file: /tmp/phpZ67YXk Stored in: upload/images.jpeg
But no file is saved on the server.
I don't know what is wrong but I'm thinking in terms of permission, still there is a folder named upload with 777 permissions.
These php-files are hosted on a online web host so I don't run this locally.
HTML-form
<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>
</body>
</html>
upload_file.php
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_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"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
$_FILES[“file”][“name”] – the name of the uploaded file. $_FILES[“file”][“type”] – the type of the uploaded file. $_FILES[“file”][“size”] – the size in bytes of the uploaded file. $_FILES[“file”][“tmp_name”] – the name of the temporary copy of the file stored on the server.
The default values for PHP will restrict you to a maximum 2 MB upload file size.
enctype="multipart/form-data": This value refers to the content type of the files that will be accepted for uploading. It also indicates the type of encoding that the PHP script will use for uploading. The multipart/form-data value enables us to upload files using the POST method.
I suppose the problem is in your relative path of destination (relative according to your current working directory = path of your .php file), try to make it absolutem like this:
move_uploaded_file(
$_FILES["file"]["tmp_name"],
$_SERVER['DOCUMENT_ROOT'] . 'upload/' . $_FILES["file"]["name"]
);
this help you:
chmod 777 foldername
php code:
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png", "mp3",
"acc", "wav", "3gpp", "mp4", "3gp", "m4a", "amr", "avi",
"flv", "gif");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152666655){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"/var/www/upload/users/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
html:
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With