Why doesn't $_POST recognise certain files uploaded? - php

You would not believe this fault, I have no idea what's going on. When I try to upload files, some of them refuse to upload.
<html>
<body>
<?php
var_dump($_POST);
if ($_POST['fileadd']){
echo "Type: " . $_FILES["file"]["type"] . "<br />";
print_r($_POST); echo "<br>"; print_r($_FILES);
}
?>
<form action="" method="POST"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" class="bigtext" style="width: 80%;">
<br>
<input type="submit" class="bigbutton" id="showloader" name="fileadd" value="Upload the song" />
</form>
</body>
</html>
Here it is in action.
The problem is when it says array 0 which it shouldn't say.
Also, how come when I choose certain files, the $_POST array is nil/zero, when it should at least have fileadd in it, as the name of the submit button?

After seeing full code and conversing with the OP on finding a solution to the problem at hand, have concluded the problem to be the following points:
This:
if ($_POST['fileadd']){
Needed to be changed to:
if(isset($_POST['fileadd']))
in order to check if the Submit button has been set.
Also the upload max size was set too low in accordance with the size of files attempted to be uploaded, using the following in .htaccess to increase it.
php_value upload_max_filesize 48M
php_value post_max_size 48M

You're simply assuming that your code is perfect. It's not. It assumes that file uploads ALWAYS succeed, and doesn't allow for the possibility of failure. You need to have something more like:
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
die("File upload failed with error code #" . $_FILES['file']['error']);
}
The error codes are defined here: http://www.php.net/manual/en/features.file-upload.errors.php

Related

Can't upload photo in database [duplicate]

I've attempted to write code to have a file uploaded to a "media" folder in PHP. For some reason it continues to not work.
Here's the execution code:
move_uploaded_file($_FILES["file"]["tmp_name"],"../media/" . $_FILES["file"]["name"]) or die ("Failure to upload content");
Here's my form code:
<input type="file" name="file" id="file" />
Any ideas why it may not be working?
EDIT:
When I use the command "print_r($_FILES);", it displays:
Array ( [file] => Array ( [name] => Screen Shot 2012-05-29 at 12.36.11 PM.png [type] => image/png [tmp_name] => /Applications/MAMP/tmp/php/phpHNj3nW [error] => 0 [size] => 71640 ) )
Image is NOT uploaded into the folder.
Make sure that in your form.. you put the enctype.
eg: <form method="post" enctype="multipart/form-data" action="index.php"></form>;
To check if files are successfully updated upon submitting the form. use print_r to see results. print_r($_FILES);
make sure media folder has 777 permission and the path ../media/ is correct
Put the encription in form
<form method="post" action="index.php" enctype="multipart/form-data"></form>
Check in php.ini file max_execution_time
heavy images are not uploaded due to execution time..
Check in php.ini file max_execution_time heavy images are not uploaded due to execution time..
eg: ;
There is a form with encrypt type or an ajax call? Do you check if the file is sended to the upload script (with a print_r($_FILES["file"]).
If correct, do you have check if the relative path is correct?
You must start from the current script (if file is included you must start from the including script).
Sorry if answer seems simply, but the posted code is a little too short to evaluate.
In your form tag you want something like this
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Make sure enctype is set to multipart/form-data for files. Replace uploader.php with the name of the php file doing the processing. Also make sure your permissions are set so the file can be created in the directory.
Here's a list of possible problems: http://php.net/manual/en/features.file-upload.php
Have you checked that the "web server user" has write permissions to "../media" ?
I was having the same problem. I am using ubuntu 18.04 and it was solved when i used this permission command on terminal.
sudo chmod -R 777 /var/www/html/target_dir.
->I have apache2 web server and target_dir as Download so replace target_dir as per your destination directory.
Make sure that in your form.. you put the enctype.
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success
If anyone wounder what's the simplest file php uploader this is the code I test it,
it will upload the file to current folder
IT'S NOT SECURE
This code for learning purpose only, don't upload it to working environment.
<html>
<body>
<form method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file1" id="file1" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
if ($_FILES["file1"]["error"] > 0) {
echo "Error: " . $_FILES["file1"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
echo "Type: " . $_FILES["file1"]["type"] . "<br />";
echo "Stored file:".$_FILES["file1"]["name"]."<br/>Size:".($_FILES["file1"]["size"]/1024)." kB<br/>";
move_uploaded_file($_FILES["file1"]["tmp_name"],dirname(__FILE__).'/'.$_FILES["file1"]["name"]);
}
}
exit ();
?>
credit https://stackoverflow.com/a/15709181/3019002
You need to set the folder permission to 777. otherwise your file won't load

Server freezes when uploading large sized images in php

I have simple html form with post method, which sends an image to my action.php file:
<form action="action.php" method="post" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="avatar"/>
<input type="submit" name="submit" value="Create"/>
</form>
And my action.php file looks like this:
if(isset($_POST['name'])) {
$error = $_FILES['avatar']['error'];
echo $error;
}
It works fine with small sized images, but when I try to submit images with approximately 2mb size the server just stops responding. It even doesn't send post request to my action.php.
Note: I've already set my upload_max_filesize = 64M, but the same thing is happening. It doesn't show any errors
Change the value of post_max_size = 70M.
Must be greater than or equal to upload_max_filesize.
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.
If problem persist you could try these solutions (1,2,3).

file uploading in php not working for more than 20kb (i looked up all possible questions here)

<form action="uploads.php" enctype="multipart/form-data" method="post">
<?php
if(!empty($message)){
echo "<p>{$message}</p>";
}
?>
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<label for="something">Please, upload your file</label>
<input type="file" name = "file_upload" id="something"/>
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
I changed post_max_size = 100M, max_file_size was already 64M. Problem is when I run the code and upload more than 20kb then first time it gives error no. 3 (partial upload problem) and then stop working. I have to restart php to do other thing. Please help..
In your php.ini file, change this variable upload_max_filesize to whatever max_uploaded_size you wish to have.
But remember, to keep your post_max_size greater than or equal to upload_max_filesize.
For more info, see this answer.
PHP change the maximum upload file size

Empty FILES array for large (ish) file uploads

I've got some legacy code that for the life of me I cant see why it's suddenly stopped working.
Very basic upload script:
if($_FILES['csvfile']['name']){
//if no errors...
if(!$_FILES['csvfile']['error']){
//now is the time to modify the future fle name and validate the file
$new_file_name = strtolower($_FILES['csvfile']['tmp_name']); //rename file
//move it to where we want it to be
move_uploaded_file($_FILES['csvfile']['tmp_name'], 'active_leads1.csv');
//echo 'Congratulations! Your file was accepted.';
}else{
//set that to be the returned message
//echo 'Ooops! Your upload triggered the following error: '.$_FILES['csvfile']['error'];
die("Unfortuanatly there was an error: {$_FILES['csvfile']['error']}");
}
}else{
die("Unfortuanatly there was an error: ".print_r($_FILES,true)."");
}
And equally the form is just as basic:
<form action="csvconvert.php" method="post" enctype="multipart/form-data">
<input type="file" name="csvfile" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
And yet, it fails on a file c. 11MB. Smaller files are ok.
Checked the post limit and upload limit, both are fine (256M and 128M), max input time is 240, and yet if I print_r $_POST and $_FILES they are both empty.
Has anyone come across this before? Any help would be appreciated!
If smaller files are ok, are you sure your upload_max_filesize is larger than 11MB? What is the value of upload_max_filesize when you use echo phpinfo();.

File not uploading PHP

I've attempted to write code to have a file uploaded to a "media" folder in PHP. For some reason it continues to not work.
Here's the execution code:
move_uploaded_file($_FILES["file"]["tmp_name"],"../media/" . $_FILES["file"]["name"]) or die ("Failure to upload content");
Here's my form code:
<input type="file" name="file" id="file" />
Any ideas why it may not be working?
EDIT:
When I use the command "print_r($_FILES);", it displays:
Array ( [file] => Array ( [name] => Screen Shot 2012-05-29 at 12.36.11 PM.png [type] => image/png [tmp_name] => /Applications/MAMP/tmp/php/phpHNj3nW [error] => 0 [size] => 71640 ) )
Image is NOT uploaded into the folder.
Make sure that in your form.. you put the enctype.
eg: <form method="post" enctype="multipart/form-data" action="index.php"></form>;
To check if files are successfully updated upon submitting the form. use print_r to see results. print_r($_FILES);
make sure media folder has 777 permission and the path ../media/ is correct
Put the encription in form
<form method="post" action="index.php" enctype="multipart/form-data"></form>
Check in php.ini file max_execution_time
heavy images are not uploaded due to execution time..
Check in php.ini file max_execution_time heavy images are not uploaded due to execution time..
eg: ;
There is a form with encrypt type or an ajax call? Do you check if the file is sended to the upload script (with a print_r($_FILES["file"]).
If correct, do you have check if the relative path is correct?
You must start from the current script (if file is included you must start from the including script).
Sorry if answer seems simply, but the posted code is a little too short to evaluate.
In your form tag you want something like this
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Make sure enctype is set to multipart/form-data for files. Replace uploader.php with the name of the php file doing the processing. Also make sure your permissions are set so the file can be created in the directory.
Here's a list of possible problems: http://php.net/manual/en/features.file-upload.php
Have you checked that the "web server user" has write permissions to "../media" ?
I was having the same problem. I am using ubuntu 18.04 and it was solved when i used this permission command on terminal.
sudo chmod -R 777 /var/www/html/target_dir.
->I have apache2 web server and target_dir as Download so replace target_dir as per your destination directory.
Make sure that in your form.. you put the enctype.
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success
If anyone wounder what's the simplest file php uploader this is the code I test it,
it will upload the file to current folder
IT'S NOT SECURE
This code for learning purpose only, don't upload it to working environment.
<html>
<body>
<form method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file1" id="file1" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
if ($_FILES["file1"]["error"] > 0) {
echo "Error: " . $_FILES["file1"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
echo "Type: " . $_FILES["file1"]["type"] . "<br />";
echo "Stored file:".$_FILES["file1"]["name"]."<br/>Size:".($_FILES["file1"]["size"]/1024)." kB<br/>";
move_uploaded_file($_FILES["file1"]["tmp_name"],dirname(__FILE__).'/'.$_FILES["file1"]["name"]);
}
}
exit ();
?>
credit https://stackoverflow.com/a/15709181/3019002
You need to set the folder permission to 777. otherwise your file won't load

Categories