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
Related
I have a form that uses JQuery to auto submit the form immediately after the file has been selected.
The image is uploaded and displays correctly but no data is inserted into the table.
I have tried removing the 'if isset' to test the SQL inserts data correctly and it does but obviously the if statement is needed so it doesn't insert data to the table every time the page is loaded.
I assume the problem is because I have removed the forms submit button however if I add it back in the form no longer auto submits after image has been selected.
$('#file').change(function() {
$('#target').submit();
});
if(isset($_FILES['file'])){
DB::query('INSERT INTO images VALUES(\'\', :image, :img_id)',array(':image'=>$image, ':img_id'=>$userid));
}
<form id="target" action="upload.php" name="target" method="POST" enctype="multipart/form-data">
<img src="uploads/profileu<?=$userid?>u.jpg?=<?php echo rand() . "\n";?>">
<label class="btn btn-primary glyphicon glyphicon-pencil">
<input type="file" id="file" name="file" onchange="form.submit()" style="display:none">
</label>
</form>
I should also note the images are uploaded to and displayed from a local folder (working correctly) and the the SQL is to simply insert a 1 or 0 into database (user has image or does not have image)
The underlying issue was that I had the form action="upload.php" which is where the PHP for uploading images was all stored. Which meant I had to move my if(isset) statement to the top of that page instead of the page where the form was. Now it works perfectly without a submit button.
Because you're inserting $image and $userid you should check if those variables are set.
Like so:
if (isset($image) && isset($userid)) {
// insertion code
}
You have to use $_FILES['fileName'] not $_FILE
To Check if a file has been successfully uploaded use.
if($_FILES["fileName"]["error"] == 0){ ... }
Along with this you can check if the uploaded file is an image you can check it as:
if(getimagesize($_FILES["fileName"]["tmp_name"])){
//Its an Image
}
else
{
//Invalid Image
}
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.
I am trying to upload images on a form but I am using Jquery .Post function in order to submit the data of the form. I get a PHP error of an undifined index. Here is a small portion of my code:
related HTML:
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Picture: <input name="uploadedfile" id="uploadedfile" type="file" />
related jQuery
$.post("registerCB.php", {
uploadedfile: $("#uploadedfile").val()
}
The PHP that handles the submission:
//file upload
$uploadedfile= $_POST["uploadedfile"];
/*--------------------Image Uploads-------------------------*/
// Where the file is going to be placed
$target_path = "userImages/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES[$uploadedfile]['name']);
THE ERROR:
CONCLUSION:
I think the issue is the .val() on the image input. I did an alert on that element and it would only alert the file name NOT the entire path.
How can I get the entire path?
ONE MORE THING----
I would like to control the NAME of the file. So no matter what the user uploads I can control the name....is this possible?
THANKS!!!
You should read the PHP documentation about file uploading using POST.
you cant read the value of <input type='data'/> and then post some information via jQuery.
you need to post the form via jQuery the <input type='data'> is within!
<form type='post' id='myForm' enctype="multipart/form-data">
<input type='file' name='uploadedFile'/>
</form>
$("myForm").attr("action", "registerCB.php").submit();
and about the reading of the data via php, I would refer to the php.net article
The browser does not include the full path for security reasons. And you can control the name of the file on the server.
If you want to upload asyncronously you should look at some of the existing jquery plugins such as:
http://www.uploadify.com/
or
http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
For Target path you have to use the following:
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
I have a form where I'm posting different fields and every type of field posted seems to work except the input File type.
I'm using var_dump($_POST); and all the other fields are there but nothing in the input type file.
My form part looks like this:
<form enctype="multipart/form-data" id="ajax-form" action="index2.php" method="POST" data-ajax="true">
and works well for everything else.
If there anything that's different in the input type file?
<input type="text" id="myid" name="myid" value="" /> ..This posts value
<input id="theimage" name="theimage" type="file" /> .. does not post value
Any ideas anyone?
Files are stored in $_FILES, not $_POST
http://php.net/manual/en/reserved.variables.files.php $_FILES variable
http://www.php.net/manual/en/features.file-upload.php Manual on PHP File Uploads.
To handle the file (no error checking):
$ROOT = "/path/to/store/files";
foreach($_FILES as $file => $details)
{ // Move each file from its temp directory to $ROOT
$temp = $details['tmp_name'];
$target = $details['name'];
move_uploaded_file($temp, $ROOT.'/'.$target);
}
See also http://www.php.net/manual/en/function.move-uploaded-file.php for more examples.
Actually if you try Firefox (>13), you can get the posted value. But if you are using Chrome or Safari, you can't get the posted value. I think the it is related with the Browsers.
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.