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
Related
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
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
I'm developing a website where multiple images are uploaded to a website and I'm playing around with ways that this can be done. At the moment I have a php script that will get & display images from a given folder which looks like this:
<?php
$dirname = "content/2014/February/";
$images = glob($dirname."*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
This works fine and I can format the <img> using css and apply jquery for the gallery, BUT, how can I upload the folder of images using php and a html form in the first place?
It is possible to upload a folder now. You can get it done by following below code:
<input type="file" webkitdirectory mozdirectory />
You can check the demo here: https://jsfiddle.net/kevalpadia/vk6Ldzae/
I hope it will help you solve your issue.
<input type="file" webkitdirectory="" directory="" /> - this works just on few/modern browsers- like Edge or Webkit engines (Chrome). I think that is not supported by Firefox.
The Current Answer is NOT supported by all browsers.
You can not upload a whole folder.
currently only chrome supports it
And to upload many files http://www.uploadify.com/
You can use HTML5's
<input type="file" multiple>
About processing uploads in PHP, read more here:
http://php.net/manual/en/features.file-upload.post-method.php
Yes, It is possible.
Here is the code:
<form method="post" enctype="multipart/form-data" action="#">
Folder Name: <input type="text" name="foldername" /><br/>
Choose Directoryy: <input type="file" name="files[]" id="files" multiple directory="" webkitdirectory="" mozdirectory=""><br/>
<input class="button" type="submit" value="Upload" name="upload" />
</form>
<?php
if(isset($_POST['upload']))
{
if($_POST['foldername']!="")
{
$foldername=$_POST['foldername'];
if(!is_dir($foldername))
mkdir($foldername);
foreach($_FILES['files']['name'] as $i=>$name)
{
if(strlen($_FILES['files']['name'][$i]) > 1)
{
move_uploaded_file($_FILES['files']['tmp_name'][$i],$foldername.'/'.$name);
}
}
echo "Folder is uploaded successfully ..";
}
else
echo "Folder uploaded Failed!!";
}
?>
Give this PHP script a go:
Main website:
http://www.uploadify.com/
Documentation:
http://www.uploadify.com/documentation/
Hi Hi trying to upload a file via a form move it to another folder and then print its name. But doesnt work don't know why.
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/");
echo $_FILES['files']['name'];
}
?>
form missing:
enctype="multipart/form-data"
ref:
spec
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/".$_FILES['files']['name']);
echo $_FILES['files']['name'];
}
?>
You need to include the file name in the upload path, like shown above.
Also, where is your upload (ajax) code?
Did isset($_FILES['files']) return true?
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].',';