I have an error when I try to upload an image / files. The image is uploaded but I have always this error.
[08-Sep-2016 09:45:29 America/New_York] PHP Notice: File Upload [POST]: Cannot process $_FILES[products_image_resize]['tmp_name'] in /home/www/boutique/includes/OM/Upload.php on line 73
Line 73 o Upload class
if ( isset($_FILES[$this->_file]) ) {
if ( isset($_FILES[$this->_file]['tmp_name']) && !empty($_FILES[$this->_file]['tmp_name']) && is_uploaded_file($_FILES[$this->_file]['tmp_name']) && ($_FILES[$this->_file]['size'] > 0) ) {
$this->_upload = array('type' => 'POST',
'name' => $_FILES[$this->_file]['name'],
'size' => $_FILES[$this->_file]['size'],
'tmp_name' => $_FILES[$this->_file]['tmp_name']);
} else {
trigger_error('File Upload [POST]: Cannot process $_FILES[' . $this->_file . '][\'tmp_name\']');
}
}
Inside my class products : function getImage()
// load originale image
$image = new Upload('products_image_resize', DIR_FS_CATALOG_IMAGES . $dir_products_image, null, array('gif', 'jpg', 'png'));
if ( $image->check() && $image->save() ) {
$error = false;
}
if ( $error === false ) {
$sql_data_array['image'] = $dir_products_image . $separator . $image->getFilename();
} else {
$sql_data_array['image'] = '';
$OSCOM_MessageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_NOT_WRITEABLE, 'warning');
}
called
// image
$this->getImage();
In my html files, my form
<form name="new_product" action="http://boutique/admin/index.php?Products&cPath=&pID=3&action=update_product" method="post" enctype="multipart/form-data">
My test
//print_r($_FILES[$this->_file]);
// Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) - no files
//Array ( [name] => shopping-bag.png [type] => image/png [tmp_name] => /tmp/phpOg7mnD933577 [error] => 0 [size] => 933577 ) - files
print_r($_FILES[$this->_file]['tmp_name']); // 0 - no files
print_r($_FILES[$this->_file]['size']); // 0 - no file;
print_r($_FILES[$this->_file]['tmp_name']); // //tmp/phpOg7mnD933577 - files
print_r($_FILES[$this->_file]['size']); // /tmp/phpOg7mnD933577g - file;
line to test
if ( isset($_FILES[$this->_file]['tmp_name']) && !empty($_FILES[$this->_file]['tmp_name']) && is_uploaded_file($_FILES[$this->_file]['tmp_name']) && ($_FILES[$this->_file]['size'] > 0) ) {
replaced by
if (!empty($_FILES[$this->_file]['tmp_name'])) { // works
if (is_uploaded_file($_FILES[$this->_file]['tmp_name'])) { // File Upload [POST]: Cannot process $_FILES[products_image_resize]['tmp_name']
if (($_FILES[$this->_file]['size'] > 0)) { //works
In your form, change
<form name="new_product" action="http://boutique/admin/index.php?Products&cPath=&pID=3&action=update_product" method="post" enctype="multipart/form-data">
to
<form name="new_product" action="http://boutique/admin/index.php?Products&cPath=&pID=3&action=update_product" method="post" enctype="application/x-www-form-urlencoded">
Related
I'm using IMAP to do some data entry by reading a mailbox. The code works perfectly for almost all emails except for one instance where the attachment's filename contains asterisk (*) character, in such case the code can't grab the file (or more exactly, the PDF attachment is not readable)
The code that I write to fetch email content (including getting PDF attachment) is:
public function fetchBody ($message_num) {
if ($this->imap) {
$body = '';
$body_type = 'text';
$attachments = array();
$structure = imap_fetchstructure($this->imap, $message_num);
//pr($structure);
if (!$structure) {
return false;
} else {
if ($structure->type == 0) {
if (strtolower($structure->subtype) == 'html') {
$body_type = 'html';
}
$body = $this->decodeBody(imap_body($this->imap, $message_num), $structure->encoding, FT_PEEK);
} elseif ($structure->type == 1) {
// Grab the text portion of a multipart email
if (count($structure->parts)) {
foreach ($structure->parts as $i => $part) {
if (strtolower($part->subtype) == 'alternative') {
if (count($part->parts)) {
foreach ($part->parts as $j => $subpart) {
if (strtolower($subpart->subtype) == 'plain' || strtolower($subpart->subtype) == 'text') {
$body = $this->decodeBody(imap_fetchbody($this->imap, $message_num, ($i + 1) . '.' . ($j + 1), FT_PEEK), $subpart->encoding);
}
}
}
} elseif (strtolower($part->subtype) == 'related') {
if (count($part->parts)) {
foreach ($part->parts as $j => $subpart) {
if ( isset($subpart->parts) && count($subpart->parts)) {
foreach ($subpart->parts as $k => $subsubpart) {
if (strtolower($subsubpart->subtype) == 'plain' || strtolower($subsubpart->subtype) == 'text') {
$body = $this->decodeBody(imap_fetchbody($this->imap, $message_num, ($i + 1) . '.' . ($j + 1) . '.' . ($k + 1), FT_PEEK), $subsubpart->encoding);
}
}
}
}
}
} elseif (strtolower($part->subtype) == 'plain' || strtolower($part->subtype) == 'text') {
$body = $this->decodeBody(imap_fetchbody($this->imap, $message_num, $i + 1, FT_PEEK), $structure->encoding);
} else {
CakeLog::write('debug', print_r($part, true));
if (($part->type >= 2 && $part->type <= 7) && (!isset($part->parts))) {
$attachments[] = $this->processAttachment($part, $i);
}elseif (strtolower($part->subtype) == 'mixed' || (count($part->parts) && $part->subtype == 'RFC822')) {
if (count($part->parts)) {
foreach ($part->parts as $j => $subpart) {
if (strtolower($subpart->subtype) == 'plain' || strtolower($subpart->subtype) == 'text') {
$body = $this->decodeBody(imap_fetchbody($this->imap, $message_num, ($i + 1) . '.' . ($j + 1) . '.' . ($k + 1), FT_PEEK), $mixedsubpart->encoding);
}else{
if ($subpart->type >= 2 && $subpart->type <= 7) {
$attachments[] = $this->processAttachment($subpart, $i);
}
}
}
}
}
}
}
}
}
}
$data['body'] = $this->cleanUp($body, $structure);
$data['body_type'] = $body_type;
$data['attachments'] = $attachments;
return $data;
} else {
return false;
}
}
Also, this is the debug output when the code detects attachment's filename contains asterisk characters:
2016-02-19 17:13:46 Debug: stdClass Object
(
[type] => 2
[encoding] => 0
[ifsubtype] => 1
[subtype] => RFC822
[ifdescription] => 0
[ifid] => 0
[lines] => 343
[bytes] => 25561
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 0
[parameters] => stdClass Object
(
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 3
[encoding] => 3
[ifsubtype] => 1
[subtype] => PDF
[ifdescription] => 1
[description] => *MEA - Invoice No. 91135811 *
[ifid] => 0
[bytes] => 23602
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => *MEA - Invoice No. 91135811 *.pdf
)
)
)
)
)
I can still save all the information (title, filename, etc) to the database though, only the file when saved to the server is unreadable for some reason (see attached photo)
Has anybody come across this situation before?
form action="upload" enctype="multipart/form-data" method="post">
<input id="file" name="file" type="file" />
<input id="Submit" name="submit" type="submit" value="Submit" />
</form>
At server side
include 'reqFunctions.php';
$uploaddir = '/path/';
$idx = "file";
$res = array("success" => true, "status" =>array());
if (isset($_FILES[$idx]) && is_array($_FILES[$idx])) {
foreach ($_FILES[$idx]["error"] as $key => $error) {
$status = array("success" => true);
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES[$idx]["tmp_name"][$key];
$name = $_FILES[$idx]["name"][$key];
$name = $_FILES[$idx]["name"][$key];
$extension=end(explode(".", $name));
$newfilename=generateRandomString(10)."_".$timestamp1."_".generateRandomString(10).$extension;
if (move_uploaded_file($tmp_name, $uploaddir.$newfilename)) {
$status["message"] = "ok";
$status["path"]="images.smsiland.com/post/large/".$newfilename;
} else {
$res["success"] = false;
$status["success"] = false;
$status["error"] = error_get_last();
$status["message"] = "internal server error";
}
} else {
$res["success"] = false;
$status["success"] = false;
$status["error"] = $error;
$status["message"] = "upload error";
}
$res["status"][] = $status;
}
}
echo(json_encode($res));
Warning: Invalid argument supplied for foreach() in /upload.php on line 12
{"success":true,"status":[]}
A foreach statement only works on arrays, your code has not caused the $FILES array to contain arrays because you have not told the HTML that you want it to be an array.
The browser is currently returning a $FILES array like this:
Array
(
[file] => Array
(
[name] => xxx.bmp
[type] => image/bmp
[tmp_name] => \tmp\php73AB.tmp
[error] => 0
[size] => 78918
)
)
because you have used this HTML statement
<input id="file" name="file" type="file" />
Note all the fields in the files array are scalar fields which is causing the foreach ($_FILES[$idx]["error"] as $key => $error) { to generate a WARNING and then NOT EXECUTE anything inside the foreach statement.
foreach ($_FILES[$idx]["error"] as $key => $error) {
$status = array("success" => true);
if ($error == UPLOAD_ERR_OK) {
. .
}
}
I am asssuming this code is just step 1 and eventually you want to allow the script to upload more than one file. If that is that case then the simple solution is to change the HTML for the <input type="file"... tag like this
<input id="file" name="file[]" type="file" />
the browser will then generate the $_FILES array like this
Array
(
[file] => Array
(
[name] => Array
(
[0] => xxx.bmp
)
[type] => Array
(
[0] => image/bmp
)
[tmp_name] => Array
(
[0] => \tmp\phpCABF.tmp
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 78918
)
)
)
And your code will compile as each fields in the $FILES array is not itself also an array.
Alternatively you could just add another
<input id="file1" name="file[]" type="file" />
<input id="file2" name="file[]" type="file" />
Which would have the same effect.
before foreach line code
because there is no array exist in $_FILES[$idx]; it returns 0
<?php
$uploaddir = '/upload/';
$idx = "file";
//echo "<pre>";print_r($_FILES);exit;
$res = array("success" => true, "status" =>array());
if (isset($_FILES[$idx]['name']) && is_array($_FILES[$idx])) {
if(is_array($_FILES[$idx]["error"])){
foreach ($_FILES[$idx]["error"] as $key => $error) {
$status = array("success" => true);
if ($error == UPLOAD_ERR_OK) {
//move upload codde goes here
}
}
}else{
//echo "your code ";
}
}
?>
I am using this PHP Code:
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if(!empty($_FILES['ticket_files']))
{
}
}
But if the file input is blank, it still thinks that there is a file there and runs the code.
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if (count($_FILES['ticket_files']) > 0)
{
}
}
Try this instead of what you have now.
try this
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if(!$_FILES['ticket_files']['error'][$key])
{
//Do Stuff
}
}
You can use this:
if (file_get_contents( $file_path ) == '')
{
// file is empty
}
You need check the contents of the file to see if it's empty.
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if(!empty(file_get_contents($path . $_FILES['ticket_files']['name']))
{
//Do Stuff
}
}
If nothing is uploaded $_FILES array looks like:
Array ( [image] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )
The error code 4 [error] => 4 indicates no file was uploaded and error code 0 indicates no error and file was uploaded. You can add below check instead of empty check.
if($_FILES['ticket_files']['error']==0) {
// file uploaded, process code here
}
Model function:
public function file_upload($folder, $allowed_type, $max_size = 0, $max_width = 0, $max_height = 0)
{
$folder = $this->path . $folder;
$files = array();
$count = 0;
foreach ($_FILES as $key => $value) :
$file_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
$file_name = $this->global_functions->char_replace($file_name, '_');
$count++;
$config = array(
'allowed_types' => $allowed_type,
'upload_path' => $folder,
'file_name' => $file_name,
'max_size' => $max_size,
'max_width' => $max_width,
'max_height' => $max_height,
'remove_spaces' => TRUE
);
$this->load->library('image_lib');
$this->image_lib->clear();
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) :
$error = array('error' => $this->upload->display_errors());
var_dump($error);
return FALSE;
else :
$file = $this->upload->data();
$files[] = $file['file_name'];
endif;
endforeach;
if(empty($files)):
return FALSE;
else:
return implode(',', $files);
endif;
}
This function is working partially. Files are being uploaded to the selected folder, but I am getting this error: You did not select a file to upload. and getting FALSE as a result? What seems to be a problem? (form is using form_open_multipart)
Please make sure that you have this name of your file tag field:
$key='userfile' //which is name of input type field name
Are any of your file inputs empty when this happens? $_FILES will contain an array of "empty" data if there is no file specified for the input. For example, my file input name is one, and I submitted it without specifying a file:
Array
(
[one] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
You should check if the file data is empty before processing the upload. PHP's is_uploaded_file() is useful for this.
I'm absolutely stuck on this... checked all related stackoverflow posts and nothing has helped. Using Phil's REST Server / Client setup in Codeigniter and can insert normal entries but cannot upload multiple images, actually even a single image.
I can't really work out how to debug the REST part, it just returns nothing.
I have this array coming in from the view:
Array
(
[1] => Array
(
[name] => sideshows.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phppYycdA
[error] => 0
[size] => 967656
)
[2] => Array
(
[name] => the-beer-scale.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phpCsiunQ
[error] => 0
[size] => 742219
)
[3] => Array
(
[name] => the-little-lace.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phpXjT7WL
[error] => 0
[size] => 939963
)
[4] => Array
(
[name] => varrstoen-australia.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phpcHrJXe
[error] => 0
[size] => 2204400
)
)
I am using this helper method I found to sort out multiple file uploads:
function multifile_array()
{
if(count($_FILES) == 0)
return;
$files = array();
$all_files = $_FILES['files']['name'];
$i = 0;
foreach ($all_files as $filename) {
$files[++$i]['name'] = $filename;
$files[$i]['type'] = current($_FILES['files']['type']);
next($_FILES['files']['type']);
$files[$i]['tmp_name'] = current($_FILES['files']['tmp_name']);
next($_FILES['files']['tmp_name']);
$files[$i]['error'] = current($_FILES['files']['error']);
next($_FILES['files']['error']);
$files[$i]['size'] = current($_FILES['files']['size']);
next($_FILES['files']['size']);
}
$_FILES = $files;
}
This function is being called within the API controller:
public function my_file_upload_post() {
if( ! $this->post('submit')) {
$this->response(NULL, 400);
}
$data = $this->post('data');
multifile_array();
$foldername = './uploads/' . $this->post('property_id');
if(!file_exists($foldername) && !is_dir($foldername)) {
mkdir($foldername, 0750, true);
}
$config['upload_path'] = $foldername;
$config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|xlsx|xls|txt';
$config['max_size'] = '10240';
$this->load->library('upload', $config);
foreach ($data as $file => $file_data) {
$this->upload->do_upload($file);
//echo '<pre>';
//print_r($this->upload->data());
//echo '</pre>';
}
if ( ! $this->upload->do_upload() ) {
return $this->response(array('error' => strip_tags($this->upload->display_errors())), 404);
} else {
$upload = $this->upload->data();
return $this->response($upload, 200);
}
}
I am happy to share my code, I did manage to get multiple files uploading no worries, but just trying to set it up with the API so I can call it from an external website, internally or an iPhone. Help would be appreciated.
Cheers
Update:
Here is the code from the API Controller:
function upload_post() {
$foldername = './uploads/' . $this->post('property_id');
if(!file_exists($foldername) && !is_dir($foldername)) {
mkdir($foldername, 0750, true);
}
$config['upload_path'] = $foldername;
$config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|xlsx|xls|txt';
$config['max_size'] = '10240';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
//return $this->response(array('error' => strip_tags($this->upload->display_errors())), 404);
return $this->response(array('error' => var_export($this->post('file'), true)), 404);
}
else
{
$data = array('upload_data' => $this->upload->data());
return $this->response(array('error' => $data['upload_data']), 404);
}
return $this->response(array('success' => 'successfully uploaded' ), 200);
}
This works if you go directly to the API from the form, but you need to put in the username and password within the browser. If I run this via the controller then it can't find the images.
This is not exactly what you asked for, but this is what I used to solve this problem. A PHP/jQuery upload widget!
http://blueimp.github.com/jQuery-File-Upload/