php/jquery multiple file uploads - php

I know there's quite a few topics on this already, and I've read quite a few of them, but to no avail. I have a form for uploading multiple files with descriptions. If the user needs more than one upload/description field, they can click a link to add another via jquery. The code for the form is:
<form action="adm.php?mode=upload" method="post">
<input type="hidden" name="article_id" value="<?php echo $article_id; ?>" />
<input type="hidden" name="new_path" value="<?php echo $new_path; ?>" />
<div id="files">
<div id="file" class="row">
<input type="file" name="file[]" id="file" /><br />
Description:<br />
<textarea name="desc[]" cols="50" rows="5"></textarea>
</div>
</div>
<div>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
Add File
A clone of
<div id="file" class="row">
<input type="file" name="file[]" id="file" /><br />
Description:<br />
<textarea name="desc[]" cols="50" rows="5"></textarea>
</div>
is appended to the files div when add_upload is clicked.
I believe this is correct usage of <input type="file" name="file[]" id="file" /> according to php.net, however the listener script at adm.php?mode=upload never receives the $_FILES array upon submission:
case 'upload' :
$path = request_var('new_path', '');
$article_id = request_var('article_id', 0);
$error = array();
$messages = array();
if(isset($_FILES['file']['tmp_name']))
{
// Number of uploaded files
$num_files = count($_FILES['file']['tmp_name']);
//create the directory if it doesn't exist already
if(!is_dir($path))
{
mkdir($path);
}
/** loop through the array of files ***/
for($i=0; $i < $num_files;$i++)
{
// check if there is a file in the array
if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
{
$messages[] = 'No file uploaded';
}
else
{
// copy the file to the specified dir
if(#copy($_FILES['file']['tmp_name'][$i],$path.'/'.$_FILES['file']['name'][$i]))
{
$messages[] = $_FILES['file']['name'][$i].' uploaded';
}
else
{
/*** an error message ***/
$messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
}
}
}
}
else
{
$messages[] = 'No files set for upload??';
}
$err_msg = $messages;
include($root_path.'pages/header.php');
include($root_path.'pages/error.php');
include($root_path.'pages/column.php');
include($root_path.'pages/footer.php');
exit;
break;
I always get the "No files set for upload??" error because the files aren't being transferred. All of the other values are sent over POST and received with no problem. What am I doing wrong?

Your form declaration in HTML needs to have the enctype property set.
<form enctype="multipart/form-data">
Then use something like livehttpheaders for firefox to see if the data is actually going across. After you add that enctype, there should be something in the $_FILES array on the php side.

Related

File upload in PHP not working?

My file upload system just isn't working properly, it doesn't return true when I call move_uploaded_file(). Here is my code, not sure if i'm just blind:
HTML:
<form action="updatecheat.php" method="POST">
<label for="version">Version:</label>
<input type="text" class="form-control" id="version" name="version" placeholder="New Version Number" />
<input type="hidden" name="referrer" id="referrer" value="coven-updates.php" />
<div class="custom-file-upload">
<label for="covenupdate">Upload "Coven.exe"</label>
<input type="file" id="covenupdate" name="covenupdate" />
</div>
<br /><br />
<button type="submit" name="submit" id="submit" class="btn btn-success waves-effect waves-light m-r-10">Update</button>
</form>
PHP:
$errors= array();
$file_name = $_FILES['covenupdate']['name'];
$file_size =$_FILES['covenupdate']['size'];
$file_tmp =$_FILES['covenupdate']['tmp_name'];
$file_type=$_FILES['covenupdate']['type'];
if(move_uploaded_file($file_tmp,"../Coven/Utilites/Update/".$file_name)){
$fn = "../Coven/Utilities/Version.txt";
$file = fopen($fn, "w+");
$size = filesize($fn);
fwrite($file, $_POST['version']);
$text = fread($file, $size);
fclose($file);
header("Location: urlhere");
} else {
header("Location: urlhere");
}
I have no clue why it isn't uploading properly. Any help is appreciated!
Thanks!
With your form tag you are missing one attribute enctype='multipart/form-data' which is must while you are working with upload file.
So, just change your form tag to this:
<form action="updatecheat.php" method="POST" enctype='multipart/form-data'>
you need to include enctype for a file ulpload to work .i.e enctype="multipart/form-data"
Try with add attribute enctype='multipart/form-data' and
File will be stored in temporary location, use tmp_name instead of name
if(move_uploaded_file($file_tmp,"../Coven/Utilites/Update/".$file_name)){

PHP Check if file is png using exif_imagetype():

I am trying to check if a file is png before upload and am getting the error
Warning: exif_imagetype(): Filename cannot be empty
It seems to work with one file for some reason but not with any others that I have tried.
The main is
$image = $_FILES['userImg']['tmp_name'];
if (checkImageValid($image))
{
uploadImage($image);
}
The checkImageValid Function is
function checkImageValid($image)
{
$valid = true;
$imgType = exif_imagetype($image); //error here
if($imgType != IMAGETYPE_PNG)
{
$valid = false;
$_SESSION["imageMessege"] = 'The image You are trying to upload is not of type png';
}
return $valid;
}
getting it from html form here
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10000" />
<input type="file" name="userImg" accept="image/x-png"/>
<input type="submit" value="Upload File" name="uploadImage" />
</form>
<input type="hidden" name="MAX_FILE_SIZE" value="10000" />
Is the error. Change the value to more, it's in bytes. 10000 bytes = ~10kb. Change to 10000000 for ~10MB.
Replace the line with this:
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
I tested it and it works fine for me with any images.

How to upload multiple files one by one

I need to upload a lot of images at once (e.g. 200 small images) through the browser. I have this code:
<?php
if(isset($_POST['Upload_files']) and $_SERVER['REQUEST_METHOD'] == "POST"){
echo count($_FILES['files']['name']); // Even if I upload more than 20 files, it still displays 20.
$target_directory = "upload/";
foreach ($_FILES['files']['name'] as $file_number => $file_name) {
$file = $_FILES["files"]["tmp_name"][$file_number];
if (mime_content_type($file) != 'image/png' && mime_content_type($file) != 'image/jpeg')
{ $error[] = 'File '.$file_name.' is not in JPG / PNG format!'; continue; }
else
{ if(move_uploaded_file($file, $target_directory.$file_name)) {$count_of_upload_file++;}}
}
}
// If files were uploaded
if($count_of_upload_file != 0){echo 'Your files ('.$count_of_upload_file.' ) were successfully uploaded.';}
// If there was an error
if (isset($error)) {foreach ($error as $display_error) {echo '- '.$display_error.'<br />';}}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple="multiple" accept="image/*">
<input type="submit" name="Upload_files" value="Upload files">
</form>
And it works. However my provider set the 'max_file_uploads' to 20 (and I'm not able to change it). (The Uploadify extension is working for more than 20 files, but I'd like to have my own small solution.) So I presume I need to add here something, which instead of 200 files at once uploads 200 files one by one (or twenty by twenty). But I don't know how to do it. (To use AJAX? -- I never used it, so I really don't know.) Thank you!
If you don't want to use ajax, you need a loop to receive files
for($i = 0; $i < count($_FILES['files']['tmp_name']); $i++){
$tmp = $_FILES['files']['tmp_name'][$i];
$name = md5(microtime());
if(move_uploaded_file($tmp, "dir/$name.jpg")){
echo "Uploaded successfully";
}else{
echo "failed";
}
}
In your form you must name all your input fields as files[], example:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple="multiple" accept="image/*">
<input type="file" name="files[]" multiple="multiple" accept="image/*">
<input type="file" name="files[]" multiple="multiple" accept="image/*">
<input type="submit" name="Upload_files" value="Upload files">
</form>
Or if you want to use ajax here is an example:
Upload multiple image using AJAX, PHP and jQuery

check if file has been uploaded by user in a form

I have a form where multiple images can be selected...
HTML
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="name" class="form-field" placeholder="First Name">
<input type="file" name="files[]" multiple accept="image/*" class="form-field">
<input type="submit" value="UPLOAD" class="button" name="submit4">
</form>
Then, in my PHP I want to detect if at least 1 file has been uploaded.... I tried this, but the code still triggers as if a file was uploaded...
if(isset($_POST['submit4']) and $_SERVER['REQUEST_METHOD'] == "POST")
{
echo "post sucessful<br>";
if(is_uploaded_file($_FILES['files']['temp_name']))
{ echo "file exist";}
else{ echo "no file";}
}
THIS always triggers the "no file" even if there is a file..
and if I try:
if(!empty($_FILES['files']['temp_name']))
it always triggers the "no file" ... I'm really confused...
You can use following code
if(!file_exists($_FILES['files']['tmp_name']) || !is_uploaded_file($_FILES['files']['tmp_name'])) {
echo "No File";
}
else{
echo "Successfully uploaded";
}
Found my answer...
if(!empty($_FILES['files']['name'][0]))
works quit well... thanks everyone.

Undefined Index error when uploading files

So, I have a form with two input elements: one to upload a file, and the other, a hidden input. Here it is:
<form action="upload.php" method="POST">
upload: <input enctype="multipart/form-data" name="pp" accept="image/png" type="file">
<input type="submit" value="go" />
<input type="hidden" name="eup" />
</form>
on my "upload.php" page (which is a different page), I get the undefined index error. Here's the code for that:
<?php
session_start();
if(isset($_POST["eup"])){
$fERR=false;
if(isset($_FILES["pp"])){ // undefined index error comes if this IF is removed...
$aExts = array("png");
$temp = explode(".", $_FILES["pp"]["name"]);
$tEXT = end($temp);
if ((($_FILES["pp"]["type"]=="image/x-png")
|| ($_FILES["pp"]["type"]=="image/png"))
&& ($_FILES["pp"]["size"]<300000)
&& in_array($tEXT, $aExts)){
if ($_FILES["pp"]["error"]>0){
$ppERR=true;
}
else{
// handle file upload here
}
}
else{
$fERR=true;
}
}
else{
$fERR=true;
}
if($fERR==true){
echo "ERROR";
}else{
echo "GOOD TO GO";
}
}
echo "<br />".ini_get("file_uploads");
?>
I've looked at a bunch of other posts and websites discussing this topic, but none of the solutions worked for me.
by the way, the output of that php is:
ERROR
1
<form action="upload.php" method="POST" enctype="multipart/form-data">
upload: <input name="pp" accept="image/png" type="file">
<input type="submit" value="go" />
<input type="hidden" name="eup" />
</form>

Categories