cannot upload file because of size - php

I'm trying to upload a file using PHP! I've tried uploading PNG,JPG,PDF,TXT files, these uploads only works when the file size is around 20kb.
When I try to upload files where its size is around 150KB, it prints $_FILE error = 3 and the file name lets say '1234.png' where the tmp_name is empty and the image itself as well! here's my code
ini_set('display_errors',1);
error_reporting(-1);
$imageTmp = addslashes($_FILES['image']['tmp_name']);
//$imageTmp = $_FILES['image']['tmp_name'];
$imageOldName = addslashes($_FILES['image']['name']);
$imageData = file_get_contents($imageTmp);
//$imageData = base64_encode($imageTmp);
echo 'image temp name: '. $imageTmp .' ';
echo 'error: '. $_FILES['image']['error']. ' ';
echo 'image name: '. $imageOldName. ' ';
echo 'image data: '. $imageData. ' ';
echo 'image type:'. $_FILES['image']['type'];
echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";
$inipath = php_ini_loaded_file();
if ($inipath) {
echo 'Loaded php.ini: ' . $inipath;
} else {
echo 'A php.ini file is not loaded';
}
I've tried multiple solutions such as changing the values of post_max_size = 200M and upload_max_filesize = 200M instead of 32M
Here's is the result of trying to upload txt file 4KB:
image temp name: /Applications/MAMP/tmp/php/phpOc7d6a error: 0
image Name: test.txt image data: hello image type:text/plain
POST:Array
(
[submit] => Record Test
)
FILES:Array
(
[image] => Array
(
[name] => test.txt
[type] => text/plain
[tmp_name] => /Applications/MAMP/tmp/php/phpOc7d6a
[error] => 0
[size] => 405
)
)
Loaded php.ini: /Applications/MAMP/bin/php/php5.6.10/conf/php.ini
the result of uploading PNG file 127KB:
Warning: file_get_contents(): Filename cannot be empty in path/test1.php on line 10 image temp name: error: 3 image name: IMG_8807.JPG image data: image type:
POST:Array
(
)
FILES:Array
(
[image] => Array
(
[name] => IMG_8807.JPG
[type] =>
[tmp_name] =>
[error] => 3
[size] => 0
)
)
Loaded php.ini: /Applications/MAMP/bin/php/php5.6.10/conf/php.ini
BTW, after a failed upload, the server crashes and display 502 Gateway and I have to restart the Apache!

As php documentation says:
UPLOAD_ERR_PARTIAL is given when the mime boundary is not found after the file data.
A possibly cause for this is that the upload was cancelled by the user (pressed ESC, etc).
Maybe:
Permissions are wrong
Not enough free space on server.
Uploading from iOS.
This error can occur when uploading a folder due to browser limitations. Happens on Mac OS X.
These are some things you could try. Hope this helps.

Related

Cannot upload image from $_FILES

I am doing a simple image upload with PHP, which I have done 100 times before. This time it isn't working. I have both the /tmp and uploads directories set to chmod 777, I have chown'd the directories to apache, I have verified that the $_FILES array is populated:
<input type="file" name="uploadimage" id="uploadimage" />
[uploadimage] => Array(
[name] => dilbert.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpkuJyyP
[error] => 0
[size] => 731602
)
Here's my code:
$response = array();
$response['path'] = '';
$response['success'] = false;
$response['error'] = '';
$targetDir = "../../../images/uploads/";
$filepath = $targetDir . $_FILES['uploadimage']['name'];
// here just for testing since it won't return below
$response['path'] = $filepath;
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['uploadimage']['tmp_name'])) {
if(move_uploaded_file($_FILES['uploadimage']['tmp_name'], $filepath)) {
$response['success'] = true;
$response['path'] = $targetDir . $_FILES['uploadimage']['name'];
} else {
$response['error'] = 'File could not be moved';
}
} else {
$response['error'] = 'File not found';
}
} else {
$response['error'] = 'Files array not found';
}
The error I am getting is the innermost one, "File could not be moved"
The path I am getting back in my AJAX call is correct, it should be domain.com/images/uploads/filename.jpg and it is...
I manually uploaded an image to the uploads directory, domain.com/images/uploads/1.jpg and then uploaded that image again witht he uploaded, and it resolves in the UI, because the image was already there, thus I know the path being passed back is accurate.
What else can I try? Images are not ending up in /tmp
Apache errors
PHP Warning: move_uploaded_file(../../../images/uploads/dilbert.jpg):
failed to open stream: No such file or directory in
/var/www/webapp/application/controllers/DocumentController.php
on line 75, referer: http://192.168.1.3/document/
PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpm4Ivrx' to
'../../../images/uploads/dilbert.jpg' in
/var/www/webapp/application/controllers/DocumentController.php
on line 75, referer: http://192.168.1.3/document/
File does not exist:
/var/www/webapp/public/images/uploads/dilbert.jpg, referer:
http://192.168.1.3/document/

photo uploading in php under centos platform

While loading the photo using the code
if (file_exists($DocRoot. $_FILES["file"]["name"]))
{
$sourcePath = $_FILES['file']['tmp_name']; // Storing source
path of the file in a variable
$image_name = $_SESSION['d'];
$targetPath = $DocRoot.$image_name; // Target path where
file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
}else{
$sourcePath = $_FILES['file']['tmp_name'];
$image_name = $_SESSION['d'];
// Storing source path of the file in a variable
$targetPath = $DocRoot.$image_name; // Target path where
file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded
file
the following error occurs:
Array ( [type] => 2 [message] => move_uploaded_file(): Unable to move '/tmp/phpihV9Ar' to '/var/www/html/test963853/q1/cas/upload/4442910001.jpg' [file] => /var/www/html/test963853/q1/cas/stage1_1.php [line] => 1960 ) /var/www/html/test963853/q1/cas/upload/4442910001.jpg
Experts state that there would be some problem with httpd.conf. What modification must be carried out in httpd.conf? Please suggest. Thank you in advance.

Superglobal $_FILES array error element

In the $_FILES array how do you determine what the [error] element means?
It is outputting a 1, which means that it encounters an error, but how can I figure out what error this is, specifically so I can deal with it?
Otherwise I am stuck and the [error] = 1 is kind of useless since it doesn't give me much information about how to remedy the situation.
(basically is there any way to extract more information out of the error array element other then the 1 value that indicates there is an error). If there is no such way to figure out what [error] is, what is another way of figuring out why my file is not uploading properly?
This is what I have tried:
<?php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
//File properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
//Work out the file extension
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array('txt', 'jpg', 'mp3', 'mpeg3', 'mpeg', 'mpeg-3', 'zip');
if(in_array($file_ext, $allowed)){
if($file_error == 0){
if($file_size <= 10000000){
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'uploads/' . $file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)){
echo $file_destination;
}
}
}
else{
echo $file_error;
}
}
}
?>
<!doctype html>
<html>
<body>
<br><br>
Click <a href='./member.php'>here</a> to go back to the member page
<br><br>
<?php
if(isset($_POST['upload'])) {
echo "<pre>";
print_r($_FILES);
echo "</pre>";
}
?>
</body>
</html>
this is what it outputs for mp3 files:
1
Click here to go back to the member page
Array
(
[file] => Array
(
[name] => soundfile.mp3
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
(1) means : The uploaded file exceeds the upload max filesize. which is 2M and I'm sure your mp3 file size is bigger than that.
To change this directive you have two options :
1 - From php.ini (if you don't know where it is, use phpinfo() function), and change upload_max_filesize = 16M, and restart your server.
2 -From your htaccess file : php_value upload_max_filesize 16M

Why do my uploaded files not exist in my filesystem and are not returned by my PHP commands?

I'm having trouble creating a form that will upload and store images.
After uploading (submitting the form), I get this with the print_r($FILES) function:
Array ( [Image] => Array ( [name] => 4HfoEtn.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\php151F.tmp [error] => 0 [size] => 70107 ) )
However, when navigating to C:\Windows\Temp\ in my file browser, the file does not exist.
Furthermore, when I run:
$tmp_image_dir = basename($_FILES['Image']['tmp_name']);
echo "<img src=\"" . $tmp_image_dir . "\">" . "<br>";
No image shows up.
Also, when I run:
$image_dir = "/images/";
$image_dir = $image_dir . basename($_FILES['Image']['name']);
if(move_uploaded_file($_FILES['Image']['tmp_name'], $image_dir)) echo "Image Uploaded Successfully" . "<br>";
I do not get a readout of "Image Uploaded Successfully"
Then, pretty obviously, when I run:
echo "<img src=\"" . $image_dir . "\">" . "<br>
I also get an image placeholder with no image.
Make sure $image_dir exists, if not create it with mkdir or manually.
You dont need to use basename in $_FILES["Image"]["name"] since its already the basename of file.
Try below code
<?php
$image_dir = "images/";
if ( !file_exists( $image_dir ) ) {
mkdir( $image_dir, 0755 );
}
$image_dir = $image_dir.$_FILES["Image"]["name"];
if ( move_uploaded_file( $_FILES["Image"]["tmp_name"], $image_dir ) ) {
echo "Image Uploaded Successfully<br>";
}
echo '<img src="'.$image_dir.'">';
?>

getimagesize() returns no data?

I get the following error when trying to upload a .JPG file.
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in H:\Programs\webserver\root\media\images\upload.php on line 43
The following is part of the upload.php file:
if(isset($_FILES['files']))
{
$cat = $_POST['cat'];
$title = $_POST['title'];
$album = $_POST['album'];
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name)
{
// get dimensions of uploaded images
echo $temp = $_FILES['files']['tmp_name'][$key]; //location of file
echo $type = $_FILES['files']['type'][$key]; //image type
echo $size = $_FILES['files']['size'][$key]; // image size
>> line 43 >>**echo $size2 = getimagesize($temp); //function to get info of file and put into array**
echo $width = $size2[0]; // first part of the array is the image width
echo $height = $size2[1]; // second part fo the array is the image width
if($type == 'image/jpeg')
{
$filetype = '.jpeg';
}else{
$filetype = str_replace('image/','',$type);
}
$random_name = rand(1000,9999).rand(1000,9999).rand(1000,9999).rand(1000,9999);
$path = 'img/'.$random_name . $_FILES['files']['name'][$key];
$thumb_path = 'img/thumb_/'.$random_name .$_FILES['files']['name'][$key];
I have another .jpg image file that I have been uploading to test which uploads fine, also .png and .gif files also upload fine.
Just to add that I tried a couple more image files of a similar size and received the same error.
The first Image file was 4.2MB the second was 5MB. Why would the file size stop it uploading?
Also when using $_FILES['files']['error'][$key]; - it returns 1.
This is what i get when using print_r:
Array ( [files] => Array ( [name] => Array ( [0] => DSCN0354.JPG ) [type] => Array ( [0] => ) [tmp_name] => Array ( [0] => ) [error] => Array ( [0] => 1 ) [size] => Array ( [0] => 0 ) ) )
Lets look at the knowns:
You can upload .jpg, .gif, .png no problem
You are having problem uploading a specific .jpg image
These knowns lead me to believe that the problem image is too big for the current allowed upload_max_filesize in your php.ini settings:
$ -> php -i | fgrep -i size
upload_max_filesize => 2M => 2M
Try increasing the size to 6M or 8M and your bigger image should work.
If you add some error handling, you can easily see what the problem is using the error code:
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name)
{
if ($_FILES['files']['error'][$key] == UPLOAD_ERR_OK)
{
// good
}
else
{
// not good, see messages on http://www.php.net/manual/en/features.file-upload.errors.php
}
}
You have to check for $_FILES['error'][$key] before accessing tmp_name - the file was not uploaded, either because it is too big or some other reason (you will find out by the value of 'error' field)
Also when using $_FILES['files']['error'][$key]; - it returns 1.
Error 1: The uploaded file exceeds the upload_max_filesize directive in php.ini.
(see http://www.php.net/manual/en/features.file-upload.errors.php)
The error message states that the filename supplied is empty.
You are using:
$size2 = getimagesize($temp);
Where do you get $temp from? Is it the foreach() and if it is, you should use $tmp_name instead.

Categories