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
Related
I have the below code which works perfectly and compressing 1 image at a time, however, I would like to have this loop so that it can handle multiple images at any one time.
<form method="post" action="" enctype="multipart/form-data">
<div class="file-upload-wrapper">
<input type="file" name="image" accept="images" multiple>
</div>
<input type="submit" value="upload" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$image_name=$_FILES['image']['name'];
$tmp_name=$_FILES['image']['tmp_name'];
$directory_name='uploads/'; //folder where image will upload
$file_name=$directory_name.$image_name;
move_uploaded_file($tmp_name, $file_name);
$compress_file="og_optimised--".$image_name;
$compressed_img=$directory_name.$compress_file;
$compress_image=compressImage($file_name,$compressed_img);
unlink($file_name); //delete original file
}
function compressImage($source_image,$compress_image) {
$image_info=getimagesize($source_image);
$compression_level = $_POST["compression"];
if ($image_info['mime']=='image/jpeg') {
$source_image=imagecreatefromjpeg($source_image);
imagejpeg($source_image,$compress_image,60); //for jpeg or gif, it should be 0-100
}
elseif ($image_info['mime']=='image/png') {
$source_image=imagecreatefrompng($source_image);
imagepng($source_image,$compress_image,3); //for png it should be 0 to 9
}
return $compress_image;
}
?>
Any help is greatly appreciated, thank you :)
I have attempted to use a loop statement but cant seem to get it working, likely my lack of PHP knowledge.
This question already has answers here:
Multiple file upload in php
(14 answers)
Closed 1 year ago.
Could someone please assist as I would like to upload multiple images to the server as well the image names to the database.
The below code works for uploading the images to the server:
View:
<form action="<?php echo URLROOT; ?>/posts/add" method="post" enctype='multipart/form-data'>
<input type="file" name="file[]" multiple>
<input type="submit" name=submit value="Submit">
</form>
Controller:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// Count total files
$countfiles = count($_FILES['file']['name']);
// Looping all files
for($i=0;$i<$countfiles;$i++){
$filename = $_FILES['file']['name'][$i];
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'][$i], dirname(__DIR__)."/img/".$filename);
}
} else {
// Load view with errors
$this->view('posts/add');
}
}
I am also able to upload the 3 image names when using the below code with 3 separate inputs:
View:
<form action="<?php echo URLROOT; ?>/posts/add" method="post">
<input type="file" name="image1">
<input type="file" name="image2">
<input type="file" name="image3">
<input type="submit" name=submit value="Submit">
</form>
Controller:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// Sanitize POST array
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$data = [
'user_id' => $_SESSION['user_id'],
'image1' => trim($_POST['image1']),
'image2' => trim($_POST['image2']),
'image3' => trim($_POST['image3']),
];
// Validated
if($this->postModel->addPost($data)){
flash('post_message', 'Post Added');
redirect('posts');
} else {
die('Something went wrong');
}
} else {
$data = [
'image1' => '',
'image2' => '',
'image3' => ''
];
$this->view('posts/add', $data);
}
}
Could someone please advise on how I could combine the code, in order to use a single file input for uploading the images to server, as well as the image names to the database?
Complete Code
<form method='post' action='' enctype='multipart/form-data'>
<input type="file" name="file[]" id="file" multiple>
<input type='submit' name='submit' value='Upload'>
</form>
PHP
<?php
if(isset($_POST['submit'])){
$imageName =$_FILES['file']['name'];
// Count total files
$countfiles = count($_FILES['file']['name']);
// Looping all files
for($i=0;$i<$countfiles;$i++){
$filename = $_FILES['file']['name'][$i];
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'][$i],'upload/'.$filename);
//insert code
$query = "insert into images(images) values('".$imageName."')";
mysqli_query($con,$query);
}
}
?>
Use this input code -
<input type="file" name="image[]" id="file" multiple>
And count file in php file then use loop for iterating file like-
$counts = count($_FILES['image']['name']);
for($i=0;$i<$counts;$i++){
//uploading code here
}
I am facing an issue with the below code, I can upload multiple files but still need that the if condition not be executed till user chooses a file as it is not good to see Unique ID and the echo message run however no files chosen!
PHP
<?php
$path = "Uploads/Files/";
if( (isset($_POST['submit'])) && (!empty ($_FILES['myFile']['name'])) ){
$countfiles = count($_FILES['myFile']['name']);
for($i=0;$i<$countfiles;$i++){
$filename = uniqid(). $_FILES['myFile']['name'][$i];
// Upload file
move_uploaded_file($_FILES['myFile']['tmp_name'][$i], $path.$filename);
echo "<h3> $filename has been uploaded Successfully!</h3>";
}
}
?>
So I want the && part above to be valid as well and not below message to appear till files be chosen.
HTML
<form method='post' enctype='multipart/form-data'>
<input type="file" name="myFile[]" id="file" multiple>
<input type='submit' name='submit' value='Upload'>
</form>
Error:
there are two parts:
add validation for required field <input type="file" name="myFile[]" id="file" multiple required>
render whatever you want instead of echo "<h3> $filename has been uploaded Successfully!</h3>";
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
Hello I'm doing form upload 4 files field. Each field can upload 1 file.
but I just know that hacker can hack with firebug to add html5 "multiple" attribute.(see the screenshot)
and then hacker can upload exceed 4 files now that I don't want to happen.
Question : How can I do to limit the upload file to 4 files?
here is my current code
for($i=0;$i<count($_FILES["file"]["name"]);$i++) {
if($_FILES["file"]["name"][$i] != "") {
$split = explode('.', $_FILES["file"]["name"][$i]);
$ext = end($split);
$tempFile = $_FILES['file']['tmp_name'][$i]; //3
$targetFile = "upload/". date('Y-m-d')."_".($i+1).".".$ext; //5
move_uploaded_file($tempFile,$targetFile); //6
}
}
..
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file[]"/><br>
<input type="file" name="file[]"/><br>
<input type="file" name="file[]"/><br>
<input type="file" name="file[]"/><br><br>
<input type="submit" value="Upload"/>
</form>
Things like this must be validated server-side.
if(count($_FILES["file"]["name"]) > 4) {
// show the user an error and refuse to continue
}