I'm having trouble getting a simple php file upload script to work. What happens is I get the Thanks message on my browser, but the file isn't anywhere to be seen.
I've been watching the upload directory and /tmp with inotify to see if anything gets created all but nothing does. Here is the html div that allows selecting the file:
<div>
<p> Select a file </p>
<form id="support" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="file">Filename:</label>
<input type="file" name="logfile" id="file"><br/><br/>
<input type="submit" name="submit" value="Upload">
</form>
</div>
So then I try and upload the file:
$uploadDir = "/tmp/";
if(isset($_FILES['logfile']['name'])){
$uploadFile = $uploadDir . basename($_FILES['logfile']['name']);
print "<p>{$_FILES['logfile']['name']}</p>\n";
print "<p>{$uploadFile}</p>\n";
# Delete if already exists
if (file_exists($uploadFile)) {
unlink($uploadFile);
}
// Recieve the file
if (move_uploaded_file($_FILES['logfile']['tmp_name'], $uploadFile)) {
displayMessage("Thanks");
}
else {
# Couldnt move the file
displayMessage("Error moving file");
}
}
What happens is I get the message "Thanks" in my browser but no file. I've checked permissions, checked my nginx log - no errors at all. It looks like it is successful but nothing is being copied?
Edit:
Even this fails:
$myfile = fopen("/tmp/testfile.txt", "w") or die("Unable to open file!");
$current = "John Smith\n";
fwrite($myfile, $current);
fclose($myfile);
Basically I cant write to /tmp for some reason, even though nginx is not reporting any error. I've also tried the same script under apache, with the same results.
drwxrwxrwt 18 root root 460 Aug 20 10:56 /tmp/
It's working fine for me. can you check the upload directory? where you have kept your tmp folder? just use if the same place of the file $uploadDir="tmp/";
<?php
$uploadDir = "";
if(isset($_FILES['logfile']['name'])){
$uploadFile = $uploadDir . basename($_FILES['logfile']['name']);
print "<p>{$_FILES['logfile']['name']}</p>\n";
print "<p>{$uploadFile}</p>\n";
# Delete if already exists
if (file_exists($uploadFile)) {
unlink($uploadFile);
}
// Recieve the file
if (move_uploaded_file($_FILES['logfile']['tmp_name'], $uploadFile)) {
echo "Thanks";
}
else {
# Couldnt move the file
echo "Error moving file";
}
}
?>
<div>
<p> Select a file </p>
<form id="support" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="file">Filename:</label>
<input type="file" name="logfile" id="file"><br/><br/>
<input type="submit" name="submit" value="Upload">
</form>
</div>
Related
Well pretty simple question.. But can't get it right for some reason.
What would be the html code to send a file to this?
move_uploaded_file($FILES["upload"]["tmpname"], $_POST["name"]);
Here's mine but when I used it and echo/vardump everything, I only have 'name' and not the file
<form action="uploader.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="upload" id="upload">
<input type="text" name="name" id="name">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Thank you
i try to add a comment but i can't
first check if upload permission is on in php.ini
file_uploads = On
If it set to on check your upload directory which you added in uploader.php file and use if to check $_FILES['upload'] is empty
this is a simple code to uploader.php file
<?php
if(!empty($_FILES['upload']))
{
$path = "upload/"; /// directory to upload
$path = $path . basename( $_FILES['upload']['name']);
if(move_uploaded_file($_FILES['upload']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['upload']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}else {
echo 'No file selected to upload ';
}
I'm trying to upload an xml file using an Android app I developed.
To do this, I used a free hosting space, and it works great. But now, I want to upload files to one of my pc's folders, using xampp as a web server.
The problem is that when I try to upload files here something goes wrong.
I am sure my php code is good, because it works with the hosting service I have my website on, so I only changed the path, as you can see below.
For hosting service:
<?php
if (is_uploaded_file($_FILES['transactions']['tmp_name'])) {
$uploads_dir = '/membri/cendav/gestione_magazzino/ExportData/';
$tmp_name = $_FILES['transactions']['tmp_name'];
$pic_name = $_FILES['transactions']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
}
else{
echo "File not uploaded successfully.";
}
?>
Now, the code for xampp, which is the same:
<?php
if (is_uploaded_file($_FILES['transactions']['tmp_name'])) {
$uploads_dir = 'exportdata/';
$tmp_name = $_FILES['transactions']['tmp_name'];
$pic_name = $_FILES['transactions']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
}
else{
echo "File not uploaded successfully.";
}
?>
As you can see, I only changed the $uploads_dir. The path from where I access the file upload.php, from my application http://10.0.0.202:1024/Warepad/.
I also changed the permissions of the exportdata/ folder, to everyone, but it still doesn't work.
P.S. I know there's a ton of these issues, but I still can't find what the problem is.
try this:
<form action="fileupload.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>
So, now it seems to work. It's strange, because the file upload.php is the same, and I haven't changed anything else!
?php
if (is_uploaded_file($_FILES['transactions']['tmp_name'])) {
$uploads_dir = 'exportdata/';
$tmp_name = $_FILES['transactions']['tmp_name'];
$pic_name = $_FILES['transactions']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
}
else{
echo "File not uploaded successfully.";
}
?>
The Android app, now, is able to upload file correctly, but if I try to upload from an html form, it still doesn't work.
Here's the html code:
<html>
<head>
<title>Upload</title>
<meta charset="utf-8">
</head>
<body>
<form action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
Invia questo file: <input name="transactions" type="file">
<input type="submit" value="Invia File">
</form>
</body>
</html>
UPDATE
I have now solved it, the problem was that I did not have the preciding '/' and I did not have the '/' at the end, here is the final syntax of the folder path: $folder = '/home/dl/www/uploads/';
UPDATE
I have updated the code so that is can output some more detailed debugging information, and this is what is output:
Warning: move_uploaded_file(home/dl/www/uploads/test.txt): failed to open stream: No such file or directory in /home/dl/public_html/file-upload/upload.php on line 9
I have the below script which is attempting to upload a selected file to a specified directory on my web server. However, it is not uploading; I have checked that the permissions on the directory is at '777' as per numerous tutorials have suggested, but it is still throwing the 'File is not uploaded' message.
Does anyone have any suggestions as to why this may not be working?
Thanks!
<?php
$folder = "/home/dl/www/uploads";
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
Echo "File uploaded";
} else {
Echo "File not moved to destination folder. Check permissions";
};
} else {
Echo "File is not uploaded.";
};
?>
The HTML form:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
Try
$folder = "/home/dl/www/uploads/";
instead of
$folder = "/home/dl/www/uploads";
That is, a slash at the end.
I am trying to upload a file via a form and a php file. I have used the same method many times throughout my website with no problems, however this time I just can not get it to work. On my form I have this....
<span class="purple"><strong>Upload Your Image</strong></span>
<input name="userfile" type="file" id="userfile" class="textbox">
<br /><br />
<label>
<input name="submit" type="submit" class="createbutton" id="submit1" value="ADD TO BASKET">
</label>
In my php file I have this...
//upload image first
$uploaddir = '../images/';
$uploadfile = $uploaddir. $_FILES['userfile']['name'];
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$status = 1;//uploaded
$data["printfile"] = $_FILES['userfile']['name'];
} else {
$status = 0;//cant upload
echo "Upload Failed!\n";
$err = "";
}
Every time it just gives me the Upload Failed error message. Any ideas?
Ah right, I really feel like a fool now. I somehow managed to miss enctype="multipart/form-data" on my form tag. Thanks so much for your help, works perfectly now.
I am unable to successfully upload an image/file to my server. The php is as follows:
//This is the directory where images will be saved
$uploadDir = "./";
$uploadFile = $uploadDir . basename( $_FILES['photo']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadFile)){
echo "The file has been uploaded successfully.";
} else {
print_r($_FILES);
}
I chose the directory at which this script lives, to ensure the functionality before I upload to the final directory. I want to upload photo's, and will check for file extensions later - but for now I at least need the upload functionality to work.
I get an empty array returned.
The form is as follows:
<form id="imageUploadForm" name="imageForm" enctype="multipart/form-data" action="imageController.php">
<label for="photo" class="blogLabel">Upload an Image</label>
<input type="file" name="photo" id="imageUpload">
<input type="submit" name="submit" id="imageSubmit" class="btn btn-primary" value="Upload">
</form>
You forgot the most important thing about a form -- the method!
If there is no method set it defaults to get.
You want post!
Add method="post" to your form.