Posting an unknown number of files to php - php

I am currently working on php project where I need to upload files, but I have no way of knowing how many files the user will upload.
There will be one file upload by default on the form with a link to add another file upload. When the form is submitted how would I post all of the files to the php script if I don't know the number of files that are being uploaded.
Does it work in the same way as a checkbox array so you could have something like
<input type="file" name"myFile[]" />
<input type="file" name"myFile[]" />
Thanks for any help you can provide.

Yes, works fine.
try to do this, after post form with files:
<pre>
<?php print_r($_FILES['myFile']['tmp_name']); ?>
</pre>
You will get a array. Access each file info with their index:
$_FILES['myFile']['tmp_name'][0];
$_FILES['myFile']['tmp_name'][1];

Yes, it works exactly like a checkbox array.

Related

Retrieve file path through browse input button

I need to get the full file path including file name information to handle later with PHP.
I am running into many difficulties.
We can use a html form with the html
<form method="post" enctype="multipart/form-data">
<input type="file" />
However, to pass this information we need to the server, we actually have to upload the file, I do not want to do this as its wasteful, The reason being is that this project is just for my local machine, The file already exists on my machine.
A manual way of achieving this is to provide a input text field, and inserting the file(s) full location path here via another file navigation window(copy/paste), But this quickly becomes boring and requires to much work when using the tool often.
Any other suggestions to achieve this?
This isn't possible. This has been discussed before and the explanations they give there are good, so I won't repeat:
How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?
paste following line under file tag,your problem will be solved
webkitdirectory directory multiple
<input type="file" id="ctrl" webkitdirectory directory multiple/>

multi file form upload

now first thing is I've seen everywhere over the net what to do in the case that what I'm trying to do doesn't work, I've tried all of the solutions and they don't work, I'm obviously missing something.
I'm uploading multiple files from a form field. This works perfectly and runs some code that resizes etc deletes tmp files blah blah.
The problem is if I don't want to upload any files the upload and image processing script still runs throwing a bunch of errors.
I've tried the following... plus a bunch more with some weird variations :P
if($_FILES['gallery']['name']!=""){ // if files then...
include_once("gallery_edit_script.php");
}
and
if (count($_FILES["gallery"]["name"] > 0)){ // if files count is more than 0 then...
include_once("gallery_edit_script.php");
}
Would the fact that the gallery_edit_script.php is an include have something to do with it?
I checked the file error with...
$_FILES["gallery"]["error"]
It showed no files were selected to upload which was exactly what I wanted.
Any ideas people?
Thanks for anyone who has a look at this.
Cheers
Added HTML but like I said upload is working fine, it's when I want to post the form and not include files to upload that I want it to skip the gallery script. This is on an edit page, so the user has submitted form, data added to db and files uploaded, then comes back and wants to edit data but not upload files.
HTML (simplified as there are heaps of fields etc)
<form action="inventory_edit_lease.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
Gallery Photos <input class="input-file" type="file" name="gallery[]" id="gallery" multiple="multiple" />
<input class="button-edititem" type="submit" name="submit" id="button" value="" onclick="javascript:return validateMyForm();"/>
</form>
Sorry I didn't add HTML first time round, form works so didn't think I really needed it ;)
Few check lists...
Make sure you have named your <input type="file" /> as gallery:
<input type="file" />
Make sure the <form> tag has a method="post" and action="" to the correct URL.
Also, make sure your <form> tag has enctype="multipart/form-data" else you won't be able to upload files via that form!
We need to see the HTML Code of your file before we can suggest something. Make sure you have followed the above checklists and even then if it isn't working, post the code and let us know!
Without HTML form I'm just guessing:
for multiple file uploads with same name you must have the filed as
on server side you will receive them as $_FILES["gallery"]
$_FILES["gallery"] will be an array of elements, eg:
foreach($_FILES["gallery"] as $file){
var_export($file);
}
For those interested this is what worked :)
I got this from another thread if($_FILES['files']['name']!="") run if files, don't run if no files headache
if(!empty($_FILES['gallery']['tmp_name']))
{
include_once("gallery_edit_script.php");
}
else
{
header("Location: inventory_list_sales.php");
}
Funny thing is I tried this with another site I'm working on with almost identical code as I copied all the files and only edited small parts and it doesn't work lol
Thanks for everyone's help :)

How to remove files from upload array (before upload) via Javascript

In simplest terms, I have a file select form which allows for multiple files to be selected for upload:
<form enctype="multipart/form-data" method="post" id="image_upload_form">
<input type="file" multiple="multiple" id="image_upload" name="files_to_upload[]" onchange="ini_image_upload();" />
</form>
Once files are selected, I run a quick Ajax call to check the database if certain files have already been uploaded, make sure there's no invalid files, etc. and create an array of files that already exist/I don't want to upload.
Now, I am trying to REMOVE these unwanted, scummy files from my upload array (before the actual upload IE. form gets submitted).
How do I achieve this majestically feat?
I have one suggestion for you. It is better to use below for your
requirement for upload. Have a look onto below URL.It is really so easy to integrate into your existing system and lots of features available.
http://www.uploadify.com/
what are you going to validate files by? are you trying to remove files with like some tricky extenstions like .php, .asp, .aspx ? if so then in the form element you should add a onSubmit attribute and call a function from there. then in the function check all the file names and process them and remove them from the array then replace the upload array and then return true. it would submit the form.. I actually don't know how u can retrieve the filenames form the upload array but Im pretty sure that this solution should work.
good luck!

PHP: File upload always returning nothing

Hey guys I'm working on a file uploader and I have come across a problem. In my code I am checking to see if a file has been selected via the file upload form, here is the form code:
<form method="post" action="actions/save.php?id=<?print($id);?>" enctype="multipart/form-data">
Listing Photo: <input type="file" name="file"/>
<input class="add" type="submit" name="submit" value="Save"/>
</form>
The user selects the file to upload then clicks the "Save" button. Now in my uploading code i am trying to check if the file form has been set like this:
$file = $_POST['file'];
if(isset($file)) {
//Continue
} else {
//Go back
}
Now my problem is that even if the file input is set (File selected) it goes to the "Go back" part of the code.
Any suggestions or a different way of checking?
Any help is appreciate, Thanks.
When you upload files through form, you should have $_FILES superglobal array with that file, so try
print_r($_FILES['file'])
to see what it cointains (size, error code, path ...)
Uploaded files end up in $_FILES, not in $_POST
see: http://nl.php.net/manual/en/reserved.variables.files.php for documentation and examples
You should have access to uploaded files using the $_FILES array. See also the reference documentation.

How to upload .mp3 files with PHP?

<form enctype="multipart/form-data">
<input type="file" name="mp3" />
<input type="submit" />
</form>
I tried the above,and found var_dump($_FILES); is always empty.
It only works when you upload text files or images.
UPDATE
I added method="POST" and it works.Why is POST necessary here?
MP3 file uploads should work like any other file upload, there's no discrimination by file type or extension.
Check whether your file is not larger than allowed.
PHP manual on file uploads
PHP manual on file uploads: Common pitfalls
Update: #Adhip Gupta solved it. GET seems to be the default method for a FORM, not POST as I thought. Check here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1
This attribute specifies which HTTP method will be used to submit the form data set. Possible (case-insensitive) values are "get" (the default) and "post". See the section on form submission for usage information.
Did you specify the form method to be POST explicitly and try?
First thing to check is how big the files are, and what the max upload size in PHP.ini is set to.
Nothing wrong with your PHP code. And neither PHP nor the webserver know the difference between a MP3 file and other types of content.
Have you checked its not size related?
Do you know that there isn't other things between the browser and the PHP which might be filtering?
Have you tried using a wiretap (e.g. wireshark) to confirm the data is leaving the browser / getting to the server?
C.
Maybe you have missed the MAX_FILE_SIZE which should be included.
<input type="hidden" name="MAX_FILE_SIZE" value="157286400" />
You should also add action="some.php" and method="POST" to <form>
OR using http://www.uploadify.com/ with ajax

Categories