multiple file upload (array) php - php

PHP script
$count = 0;
foreach ($_FILES['filesToUpload'] as $file) {
//upload process
echo $file[$count]['tmp_name'].',';
$count ++;
}
HTML
<form method="POST" action="action-here" enctype="multipart/form-data">
<input class="btn" name="filesToUpload[]" type="file" multiple="" />
<input class="btn primary" type="submit" value="Submit">
</form>
I'm doing this majorly wrong. What i'm trying to do is make it so you select the files then the php script processes it like an array?
I keep getting out puts such as 1,i,C,,,.
I know other ways to do multiple uploads, but I know this is one of the simplest.

foreach ($_FILES['filesToUpload']['error'] as $k => $error) {
echo $_FILES['filesToUpload']['tmp_name'][$k].',';
}
Tips: debug it with print_r($_FILES).

you should write it this way:
echo $file['tmp_name'][$count].',';

Related

php - How to keep subdirectories when uploading?

Currently, I'm developing an application for uploading folders and files. I have two problems:
I can't upload files by keeping the tree (subfolders and their files).Here is the code:
<form method="post" action='post_upload.php' enctype="multipart/form-data">
<input type="file" name="files[]" id="files" webkitdirectory directory multiple>
<input class="button" type="submit" value="Upload" />
post_upload.php:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
foreach ($_FILES['files']['name'] as $i => $name) {
if (strlen($_FILES['files']['name'][$i]) > 1) {
if (move_uploaded_file($_FILES['files']['tmp_name'][$i], 'upload/'.$name)) {
echo $name."<br>";
}
}
}
If this problem is resolved, then it is possible to select more than one folder?
Thanks.
There is alternative way to do it using javascript. In javascript you will get webkitRelativePath. check example here

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

PHP 'Upload File' code not working

HTML in 'index.php':
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload" accept="application/pdf, .pdf" required><br>
<input type="submit" name="submit" value="SUBMIT">
</form>
PHP in 'index.php':
<?php
if (isset($_POST['submit'])) {
$target_dir = 'docs/';
$temp_loc = $_FILES['fileToUpload']['tmp_name'];
$file = $_FILES['fileToUpload']["name"];
if(move_uploaded_file($temp_loc,$target_dir.$file)) {
?><script>alert('successfully uploaded');</script><?php
} else {
?><script>alert('error while uploading file');</script><?php
}
}
?>
The following code doesn't seem to upload any of the documents I select to the target dircetory, that is, '/docs'.
I'm using wampserver with localhost, if that is needed at all. The 'file-upload' is allowed in the 'php.ini'
I've seen many tutorials and have even done troubleshooting on Stackoverflow itself, but I can't seem to make it work.
Thanks in advance

PHP multiple file upload: Invalid argument supplied for foreach()

i'm following this tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/comment-page-1/#comments to learn how to upload multiple files via ajax.
This is my html:
<form class="form-horizontal" id="settingsChangeAvatar" method="post" enctype="multipart/form-data" action="<?php echo $AJAX."/ajaxUpload.php"?>">
<input class="input-xlarge input-file" id="settingsUploadAvatar" name="settingsUploadAvatar" type="file" multiple />
<button class="btn" id="uploadAvatarButton" type="submit">Upload</button>
</form>
And this is my ajaxUpload.php:
foreach($_FILES["settingsUploadAvatar"]["error"] as $key => $error){
if($error == UPLOAD_ERR_OK) {
$name = $_FILES["settingsUploadAvatar"]["name"][$key];
move_uploaded_file($_FILES["settingsUploadAvatar"]["tmp_name"][$key], $_SERVER["DOCUMENT_ROOT"]."/webname/".$_FILES["settingsUploadAvatar"]["name"][$key]);
}
}
echo("File uploaded");
My code should be the same as the one in the tutorial.
Thanks for helping.
Change the 'name' attribute of your input from settingsUploadAvatar to settingsUploadAvatar[].

Upload multiple text files into MySQL

I have ZERO experience coding uploading files through browser, so this part is all very new to me.
I need to give users (in fact they will be only one or two authorized users) a way to upload multiple text files (think 50-200 files) directly into a MYSQL database.
I don't want to give them FTP access, but am OK allowing them to enter files into the database.
I can figure out how to get the data from a PHP array into the MYSQL database.
What I can't figure out is how to get the contents of multiple files into the PHP array(s).
Please help out with the code.
This example should help you understand the basic idea
<?php
$fileContents = Array();
if(count($_FILES) != 0){
foreach($_FILES as $file){
$fp = fopen($file["tmp_name"], "r");
array_push($fileContents, fread($fp, $file["size"]));
fclose($fp);
}
//$fileContents now holds all of the text of every file uploaded
}
?>
<html>
<head>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="file1" id="file" />
<input type="file" name="file2" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
It first checks that there has been files posted to itself.
If there is files, it loops through each and opens them while they are in their temporary file state.
After that it reads all of the contents at once (be careful with this) using the size attribute that sent with it.
At the same time, it is pushing the contents into the array called $fileContents.
So $fileContents[0] holds the first text file and so on.
Just add more <input type="file">s to your page, and they will all appear in the $_FILES array, which you can loop to retrieve them.
However:
The structure of the $_FILES array is slightly illogical when it comes to multiple files - make sure you read the manual carefully, it is a little counter intuitive.
Make sure that your upload_max_filesize, post_max_size, max_file_uploads and max_input_time PHP.ini directives are generous enough.
See also: Handling multiple file uploads in PHP.
<!-- FORM -->
<form method="post" enctype="multipart/form-data">
<?php
for($i=1;$i<=10;$i++) //change 10 to any number for more upload fields
{
echo '<input type="file" name="files[]" /><br />';
}
?>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
//Processor
if(isset($_POST['submit']))
{
foreach($_FILES['files']['tmp_name'] as $tmp_name)
{
if(!empty($tmp_name))
{
$filecontent[] = file_get_contents($tmp_name);
}
}
//Test
echo '<pre>';
print_r($filecontent);
echo '</pre>';
}
?>
Thank you everyone who has contributed to this. I am having a very hard time choosing the answer, because I think it's a 50/50 effort by John and DaveRandom.
In case someone wants to see the end product here it is:
HTML:
<html>
<head>
</head>
<body>
<form method="post" action="test.php" enctype="multipart/form-data">
<input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP:
<?php
function rearrange( $arr ){
foreach( $arr as $key => $all ){
foreach( $all as $i => $val ){
$new[$i][$key] = $val;
}
}
return $new;
}
$fileContents = Array();
if(count($_FILES['filesToUpload'])) {
$realfiles=rearrange($_FILES['filesToUpload']);
foreach ($realfiles as $file) {
$fp = fopen($file["tmp_name"], "r");
array_push($fileContents, fread($fp, $file["size"]));
fclose($fp);
}
foreach ($fileContents as $thisone) {
echo "<textarea wrap='off'>\n";
echo $thisone;
echo "</textarea>\n";
echo "<br>----<br>";
}
}
?>

Categories