So I am trying to do a basic upload via an HTML form and a simple php script, but the move_uploaded_file function always returns false. I have run "chmod 777" on the directory (I will deal with safety more when I actually get this to work) and the "upload" directory is in the htdocs folder (actually /var/www in Ubuntu Server and Linux Mint).
Here is the form:
<html>
<body>
<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>
...and upload_file.php...
<?php
if ($_FILES["file"]["error"] == 0){
if (file_exists("upload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
}else{
if(move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/upload/" . $_FILES["file"]["name"])){
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}else{
echo "Failed to move uploaded file";
}
}
}else{
echo "Return Code: " . $_FILES["file"]["error"];
}
?>
When I try to upload a small JPEG, I get "Failed to move file". Any thoughts?
Your script needs to be able to write to the destination directory, which in this case would be /var/www/upload/ (is that the directory where you changed the permissions?). Also you are using the client's local name which could be a possible problem and security issue (not necessarily the reason here though).
When i found out that my OS was the problem i wanted to destroy my pc.
chmod ugo+rwx yourfoldername
With this way you give permission to everyone to write/read/delete to your folder.
Related
I am hosting a file upload script on an digital ocean server. I have copied line for line tutorials from all over the internet and changed my permissions using CHMOD 777. I cannot for the life of me figure out why the file won't upload.
PHP
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
HTML
<form enctype="multipart/form-data" action="upload_manager.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
The file I'm trying to upload is only a few hundred KB and is a PNG. I have tried JPEG too.
When I print out the value of
$_FILES['uploadedfile']['tmp_name']
It is empty
The value of
$_FILES['uploadedfile']['name']
is the file name.
I had a look at my php ini and everything related to uploading files is correct.
What else can I try to get this to work?
After reading the w3schools file (http://www.w3schools.com/php/php_file_upload.asp) I followed the same steps to upload the image file. However i'm not able to see the file in wampp\tmp folder. Here is my code:
<?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"]."<br>";
echo "stored in" .$_FILES["file"]["localhost/tom/upload/"]. "<br>";
}
?> <!DOCTYPE html>
<head>
<title>Uploading file</title>
</head>
<body>
<form name="f1" action="fup2.php" method="post" enctype="multipart/form-data">
<input type="file" name="f1" /><br />
<input type="submit" value="send" />
</form>
</body>
</html>
Only sessions are saved in the tmp folder, but not the image..
Please suggest.
You will have to move the file from the temp folder to an real one.
Try:
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
If you like following w3schools they explain it right under where you linked.
("Saving the Uploaded File")
Make sure the form has at least the following:
<form method="post" enctype="multipart/form-data">
Files can only be sent through POST, and the enctype needs to be multipart/form-data.
Check the PHP error log if that wasn't the issue.
You can find the location where temporary files are supposed to be saved with sys_get_temp_dir().
I write the php to upload file and scan the directory to show them as links, the scaning directoy works well I can see the text files I created in the directory, but I just can not move the local file to the desired directory .No file shows up after execution.
I think the problem may contain in this line:
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload".$_FILES["file"]["name"]);
What I really want is to upload the image to the directory in /var/www/BlueTapeLogin/upload
and my php file lives in /var/www/BlueTapeLogin/upload_image.php
How can I change the code to make things work? Thanks in advance.
Please see my full code:
<html>
<head>
<?php
try
{
if (!empty($_POST["delete"])){
$delete=$_POST["delete"];
echo"we have the command delete this file:";
echo $delete;
$file = "upload/".$delete;
echo "/n***************";
echo "you want delete :";
echo $file;
echo "***************";
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
}else{}
}catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
<?php
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload".$_FILES["file"]["name"]);
?>
<?php
$dir=scandir("/var/www/BlueTapeLogin/upload") ;
for($j=0;$j<count($dir);$j++){
echo $dir[$j];
echo"\n";
$target = $dir[$j]; // This is the file that already exists
$link = $dir[$j]; // This the filename that you want to link it to
echo "".$link."";
}
?>
</head>
<body>
<form action="upload_image.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"><br>
<label for="file">Delete</label>
<input type="text" name="delete" id="delete"><br>
<input type="submit" name="submit" value="Submit">
</form>
logout
</body>
</html>
You're missing a directory separator between upload and the filename, it should be:
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload/".$_FILES["file"]["name"]);
The permissions you show say that only root can write into that directory, and but the webserver is probably using a userid like www-user. You need to change the ownership of the directory to the webserver userid. This will have to be done by the server administrator.
More likely, there's another directory that the webserver is already allowed to write into. The server administrator should be able to tell you what directory to use.
Check user permission to upload in that directory and try to make it simple. Just simply upload a file to other directory see it is working or not than check your code go one by one step.
Check return values check whether a warning is issued
Return Values
Returns TRUE on success.
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.
I am working on a PHP project, in which I need to store a path of an image when user select an image from open dialog box from a specified directory. How can I do this? I don't know how to open the Open/Browse dialog box and how to get that path in PHP/javascript. And I want that my other form data don't flush when I open the Open/Browse Dialog.(I want to put image file's path that user has selected in my database, so I can reduce my database size.)
You can use file uploading forms with html and send the form to your PHP file to handle the file contents. When a file is sent to the server it is stored in a temporary location.
W3Schools has a good tutorial on this, the HTML becomes:
<html>
<body>
<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>
and the PHP:
<?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"];
}
?>
http://www.w3schools.com/php/php_file_upload.asp
You can put a form element by using <input type="file">
If you only want the path without uploading the file. You can use javascript.
If you post the data to the server file's info will be available to PHP but also the file will be sent to server as well.
Check the Javascript File Api examples here if you want more ..
http://www.html5rocks.com/en/tutorials/file/dndfiles/
<input type="file">
no? or i'm something missing?
For dotNetAddict (if they are still interested) and any others similarly interested, try the following link for a good explanation of how to obtain the path to a file....
http://www.w3schools.com/jsref/prop_fileupload_value.asp
While I was trying to uplaod file using php on windows machine its working fine :
<form action="php/bulk.php"
enctype="multipart/form-data" method="post">
<p>
Please specify a file below:<br>
</p>
<div>
<input id="datafile" type="file" name="datafile" size="40"><br>
<input type="submit" value="Upload File">
</div></form>
code on bulk.php :
$connector = new DbConnector();
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['datafile']['name']);
if(move_uploaded_file($_FILES['datafile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['datafile']['name']).
" has been uploaded. \n";
} else{
echo "There was an error uploading the file, please try again!";
?>
<form action="../addsamples.php">
<input type="submit" value="Go Back" />
</form>
<?php
exit (-1);
}
I tried uploading on server and its not working there ! Do Operating system play any role in this ? My server is red hat linux and test m/c is windows
The most likely problem is the file-permissions. Does the user that is running the web-server (apache I think for red hat) have write-permissions for php/uploads/?