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.
Related
I've been running the Responsive File Manager recently, both on my local machine as well as a hosted server and have received different issues for each, despite the code being the same. These issues are all related to the uploading of files - currently the tested files have all been only JPEG files, where JPEG files larger than 2 MB are not able to be uploaded.
Shared issues:
For both local machine & hosted server, if the uploaded image exceeds 8MB, then I receive the following error message:
The uploaded file exceeds the max size allowed
Local machine issues:
For the local machine only, if the uploaded image is greater than 2MB but less than 8MB, I receive the following error message:
Warning: mime_content_type(): Empty filename or path in C:\xampp\htdocs\project\webroot\3rdparty\responsive file manager\filemanager\upload.php on line 92
where line 92 refers to this specific line within a set of if statements:
if ( ! empty($_FILES) || isset($_POST['url']))
{
if(isset($_POST['url'])){
$temp = tempnam('/tmp','RF');
$handle = fopen($temp, "w");
fwrite($handle, file_get_contents($_POST['url']));
fclose($handle);
$_FILES['file']= array(
'name' => basename($_POST['url']),
'tmp_name' => $temp,
'size' => filesize($temp),
'type' => explode(".", strtolower($temp))
);
}
$info = pathinfo($_FILES['file']['name']);
$mime_type = $_FILES['file']['type'];
if (function_exists('mime_content_type')){
$mime_type = mime_content_type($_FILES['file']['tmp_name']); //line 92
}
}
I also receive this error as well:
File extension is not allowed. (#C:\xampp\htdocs\project\webroot\3rdparty\responsive file manager\filemanager\upload.php#260)
which refers to this set of if statements:
if ( ! empty($_FILES) || isset($_POST['url']))
{
else // file ext. is not in the allowed list
{
response(trans("Error_extension").AddErrorLocation(), 406)->send(); //line 260
exit();
}
}
Server issues:
On the server, those two issues are replaced by this single error:
Not enough Memory
(#/home/site/public_html/webroot/3rdparty/responsive file manager/filemanager.upload.php#241)
which refers to this set of if statements:
if ( ! empty($_FILES) || isset($_POST['url']))
{
if (in_array(fix_strtolower($extension), $ext))
{
if ($is_img)
{
$memory_error = FALSE;
if ( $extension != 'svg' && !create_img($targetFile, $targetFileThumb, 122, 91))
{
$memory_error = TRUE;
}
// not enough memory
if ($memory_error)
{
unlink($targetFile);
response(trans("Not enought Memory").AddErrorLocation(), 406)->send(); //line 241
exit();
}
}
}
}
Attempts so far:
I've looked at the following:
With the file extensions issue on my local machine, I checked the allowed file extensions in the config.php file, but both formats of jpeg were already there:
'ext_img' => array( 'jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'svg' ), //Images
Meanwhile in include\mime_type_lib.php, both formats of jpeg were already there:
$mime_types = array(
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
)
On my local machine, I increased the upload_max_filesize to 128M. Meanwhile on the server, I used cPanel's PHP settings to do the same thing. Additionally in the config.php file, I changed the MaxSizeUpload setting to match the above changes as follows:
'MaxSizeUpload' => 128,
I did also check against the latest version of config.php and upload.php to see if my versions were outdated, but that wasn't the case.
Try this:
if ( ! empty($_FILES) || isset($_POST['url']))
{
if(isset($_POST['url'])){
if(FALSE === ($temp = tempnam('/tmp','RF'))){
response(trans("Failed to create temporary file").AddErrorLocation(), 406)->send();
exit();
}
$handle = fopen($temp, "w");
fwrite($handle, file_get_contents($_POST['url']));
fclose($handle);
$explUrl = explode(".", basename($_POST['url']));
$suffix = array_pop($explUrl);
$_FILES['file']= array(
'name' => basename($_POST['url']),
'tmp_name' => $temp,
'size' => filesize($temp),
// type should be mime-type not file suffix
'type' => $suffix,
'error' => UPLOAD_ERR_OK
);
}
if($_FILES['file']['error'] !== UPLOAD_ERR_OK){
response(trans("Error upload code: ".$_FILES['file']['error']).AddErrorLocation(), 406)->send();
// error code list: http://php.net/manual/en/features.file-upload.errors.php
exit();
}
if(empty($_FILES['file']['tmp_name'])){
response(trans("Error upload").AddErrorLocation(), 406)->send();
exit();
}
$info = pathinfo($_FILES['file']['name']);
$mime_type = $_FILES['file']['type'];
if (function_exists('mime_content_type')){
$mime_type = mime_content_type($_FILES['file']['tmp_name']);
}
}
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.
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.
This question already has answers here:
Change the maximum upload file size
(24 answers)
Closed 8 years ago.
I am using the following function to upload image file and resize.
public function upload()
{
include_once 'wideimage/WideImage.php';
$ext = $this->detect_type();
$file_name = 'uploads/' . time() . $ext;
if( move_uploaded_file($_FILES['file']['tmp_name'], $file_name ) )
{
$data[] = array(
'status' => true,
);
}
else
{
$data[] = array(
'status' => false,
);
}
$image = WideImage::load($file_name);
var_dump($image);
$resizedImage = $image->resize(639, 554)->saveToFile( 'uploads/' . time() . '_small' . $ext );
$thumb = 'uploads/' . time() . "_thumb" . $ext;
$data[] = array(
'thumb' => $thumb
);
$this->session->set_userdata('thumb', $thumb);
$resizedImage = $image->resize(566, 566)->saveToFile($thumb);
echo json_encode($data);
}
I works perfectly for files smaller than 2 MB. But when i upload images greater than 2 MB it didn't work. I dump $image object created it returns nothing.
The image is uploading perfectly uploading to the server but not resizing. Pls suggest solution to it.
I am using this image library http://wideimage.sourceforge.net/
php.ini settings are
post_max_size = 32MB;
upload_max_size = 32MB;
max_execution_time = 300;
max_input_time = 100;
Edit: Image is perfectly uploading to the server problem arise only in resizing when file size is greater than 2 MB.
When file size is smaller than 2MB var_dump($image) outputs
object(WideImage_TrueColorImage)[16]
protected 'handle' => resource(48, gd)
protected 'handleReleased' => boolean false
protected 'canvas' => null
protected 'sdata' => null
while when greater than 2MB it returns nothing
You should check your configuration:
PHP change the maximum upload file size
My guess is you left the defaults and uploads only up to 2MB are allowed.
If image is uploading perfectly to the server but not resizing - check memory_limit in php.ini (or .htaccess) and try to increase it.
Your php.ini is restricting max file size. Change it modifying php.ini values:
; Maximum allowed size for uploaded files.
upload_max_filesize = 400M
; Must be greater than or equal to upload_max_filesize
post_max_size = 400M
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