ankit kumar ankit kumar - 1 year ago 44
PHP Question

uploading of file does not work in php

I am working on php web application where i upload a file on server from local machine using following code.

<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload" value="c:/passwords.txt">
<input type="submit" value="Upload" name="submit">


$target_dir = "uploads/";
$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;

if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;

if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
echo $_FILES['fileToUpload']['name']."<br>";
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.";

But above code is not working. I checked uploads folder permission

drwxrwxrwx 2 ankit ankit 4096 Aug 2 13:41 uploads

which seems to be writable. I changed my project permission using following command

sudo chmod -R 777 /var/www/myproject

But still i cannot upload the files on server. May i know where i am wrong?

Answer Source

In your comments you say that you are getting error 1 when you use


which is due to

 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',

So do this

upload_max_filesize = 'Size'; // whatever you want

Documentation about $_FILES errors