I want start saying that I'm learning and I'm trying to understand the use of $_FILES
and $file_handler
which are making me crazy into this function to upload attachments from a frontend form.
What I've discovered into mine yesterday question was that using $_FILES
into the same function makes it overwritten so some nice dude suggested to change this variable in something different like $whatever
. Both files could be managed and uploaded then but of course this is not happening yet and only files are uploaded. Having 2 form inputs relatively named:
- For images:
name="moreimages"
- For files:
name="morefiles"
Basically I've arrived at this point in my php:
if ($_FILES)
{
// Get the upload attachment files
$images = $_FILES['moreimages'];
foreach ($images['name'] as $key => $value)
{
if ($images['name'][$key])
{
$image = array(
'name' => $images['name'][$key],
'type' => $images['type'][$key],
'tmp_name' => $images['tmp_name'][$key],
'error' => $images['error'][$key],
'size' => $images['size'][$key]
);
//here I've changed the $_FILES variable into something else
$my_processed_images = array("moreimages" => $image);
foreach ($my_processed_images as $image => $array)
{
$newupload = project_images($image,$pid);
}
}
}
// Get the upload attachment files
$files = $_FILES['morefiles'];
foreach ($files['name'] as $key => $value)
{
if ($files['name'][$key])
{
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key],
'post_mime_type' => $files['type'][$key]
);
$_FILES = array("morefiles" => $file);
foreach ($_FILES as $file => $array)
{
$uploadfile = project_file($file,$pid);
}
}
}
}
and my functions look like
function project_images($file_handler, $pid)
{
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$image_id = media_handle_upload( $file_handler, $pid );
return $image_id;
}
function project_file($file_handler, $pid)
{
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$file_id = media_handle_upload( $file_handler, $pid );
update_post_meta($file_id,'is_prj_file','1');
return $file_id;
}
I understand that there could be a problem with my $file_handler
but I don't know how to manage it. What really is happening is that debugging the $more_images
is null and not considered and $_FILES
into the second loop is uploaded instead.
Can you drive me?
EDIT
My HTML form fields are like these for entire:
<input id="moreimages" accept="image/png, image/jpeg, image/gif" type="file" name="moreimages[]" >
<input id="morefiles" accept=".zip,.pdf,.rar,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.psd,.ai" type="file" name="morefiles[]" >
Furthermore My AJAX POST STATUS is OK sending these parameters for moreimages
:
Content-Disposition: form-data; name="moreimages[]"; filename="Screen Shot 2016-04-05 at 16.48.10.png"
Content-Type: image/png
PNG
and these parameters for morefiles
:
Content-Disposition: form-data; name="morefiles[]"; filename="articolo-slow-food-ararat.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
So I think the problem, once again, how these data are received into php.
$_FILES = array("morefiles" => $file);
and$my_processed_images = array("moreimages" => $image);
. You mean in the project_images and project_files functions? Thanks.