PHP: File upload always returning nothing - php

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.

Related

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 :)

Posting an unknown number of files to 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.

PHP file upload

I have a basic upload form:
<form method="post" action="" enctype="multipart/form-data" >
<input type="file" name="logo">
<input type="submit" class="button-primary" value="Upload Image">
</form>
And this is how I upload stuff (these are WordPress functions, but the question is rather php-related, so I'm asking here, not on wp-se):
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$uploaded = insert_attachment($file,$post_id);
$uploaded_src = wp_get_attachment_url($uploaded);
update_option('logo', $uploaded_src);
}
}
Now, there are two issues and I'm not sure how to fix them:
When user uploads a file and clicks "Upload image" the image is being uploaded. But if user refreshes the page the iamge is uploaded once again. and again, and again. I believe the form is sending itself after refreshing, what's the easiest way of repairing that?
As you can see my code updates only one option called "logo", how to get name of upload fields and pass it to foreach loop so I'll be able to put more upload fields on my page? I mean something like: update_option('ThisFormUploadInputID', $uploaded_src);.
Thanks a lot! :)
When user uploads a file and clicks "Upload image" the image is being
uploaded. But if user refreshes the page the iamge is uploaded once
again. and again, and again. I believe the form is sending itself
after refreshing, what's the easiest way of repairing that?
PRG pattern - on successful POST, send Location header and terminate the script. Next request from browser will be GET, and refreshing the page won't resend the form.
As for the second question, print_r($_FILES) might help.
For the second question:
You need more fields to upload more files, or some guiding data? If files so, yes like the answer before - look in var_dump($_FILES). If you need some data inputs so look in $_POST for them.
And about refreshing.
So, yes you can send Location header from php, but you can get an Headers already sent warning. So use JS then <script>document.location="www.yourdomain.com"</script>.

how to post form details including the url of a file uploaded to a csv file with php

Please could someone help with the following:
Using PHP I want to be able to post the details entered into a form to a csv file. This is quite straight forward on its own, however one of the fields in the form needs to upload a file, so the csv file needs to contain a link to where the file is saved.
Thanks
OK, it sounds to me like you have a form, one of the fields is an upload- and you want to submit the form then create a CSV from the form fields (with the upload simply showing the file location) AND upload the file?
If that is the case, handle the file upload as per normal, so the form should have (eg):
<form enctype="multipart/form-data" action="csvbuilder.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input name="uploadedfile" type="file" />
//other fields
<input type="submit" value="Upload File" />
</form>
Then in the target script of the form (csvbuilder.php):
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
To then reference the file in the CSV, you should simply echo:
"http://www.domain.com/uploads/".basename( $_FILES['uploadedfile']['name']);
The best you can do is simply have the location as per above, CSVs by default dont support links (though some programs like Excel may 'interpret' links and make them clickable if you wrap them in markup)
Files uploaded through PHP are by default destroyed after the PHP script has executed, so you will need to move the uploaded file to a pre-designated folder to save it.
You can use the function move_uploaded_file() to do this.
Whatever you give as the destination to move_uploaded_file() can then be put in to your CSV file.
When you upload the file you will need to use the function move_uploaded_file to put the file onto the server. So just use the same argument in that function as you do in the CSV.

PHP file form question

My Code :
<?php
function dbAdd($first_name , $image) {
//mysql connect database code...
mysql_query("INSERT INTO users SET first_name = '".$first_name."', image = '".$image."'");
$mysql_close($sql);
}
if($_SERVER['REQUEST_METHOD']=='POST') {
dbAdd($_POST['first_name'], $_POST['image']);
}
?>
<form enctype="multipart/form-data" method="post" action="">
First Name : <input type="text" name="first_name" >
Image : <input type="file" name="image">
<input type="submit">
</form>
The form "file" is to upload. I know that. But I wonder how to get the values so I can put the path of image in the database. The code is already working. The $first_name can already save to the database.
Thank you for the answers.
Jordan Pagaduan
The file will be uploaded to a temporary place on the server when the form is submitted.
Once the form has been submitted, the $_FILES variable will contain all the files submitted. In your case, you could access the uploaded file using $_FILES['image']. Most likely you will want to move the file out of the temporary directory to a safer place.
For more info, have a look at the PHP manual on the topic, specifically the page on handling POST uploads. That second page has an example for you on how to move the uploaded file (have a look at the move_uploaded_file() method).
Straight from W3C: Upload form & $_FILE variable

Categories