jquery PHP image upload (using .post method) - php

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']);

Related

Get full path of selected file in PHP

I want to select file with an input tag, but this don't return me full path of selected file(full directory).
I try this code in edge browser and return me full path of selected file but other browser can't.
this my form :
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
and
$filename = $_FILES["file"]["name"];
$filedir = $_FILES["file"]["tmp_name"];
any idea??
plz help
So this is what i understand from your Question.
You need full path for the file to **open** it or work on it:
if(isset($_FILES["file"])){
$FileData file_get_contents($_FILES["file"]["tmp_name"]);
echo $FileData;
//Data inside the file will be shown in the browser
}
You need "**real**" path use:
if(isset($_FILES["file"])){
echo realpath($_FILES["file"]["tmp_name"]);
}
If you mean path from **client** **side**:
This is PHP. PHP is server side. it means the data and everything will is working by the server/computer and the browser has no power to edit it. It receives only data and the server will work with it(with what you coded)
If you want to open the file without sending it to the server:This answer will help you
Hope i get what you want

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.

Calling jrxml file from php for pdf generation

i have to generate jasper reports in my php website.
I want to know what all are needed to make this work. I was told that i needed php-java bridge. But i hope that is for the sole purpose of generating a .jrxml file.
I already have the jrxml file with me.
Now how can i call this file from my php code, for generating jasper report in pdf format ?
In case this file is located on your server you can open it using file_open from any php script.
EDIT: The easiest way to do it seems to be using something like the php-jasper-integration. This way you don't need to use a java-php bridge.
In case you do not have it onyour server but client side only you will need to upload it to your web site and treat this file as you need to using your website script.
Here is some example code to do it:
if(!isset($_FILES['userfile']['tmp_name'])){
// starte Session
//session_start(); // Headers sent out
?>
<form enctype="multipart/form-data" action="myscript.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />
<!-- Name of input element determines name in $_FILES array -->
Please upload a file.
<br>
<br>
<input name="userfile" type="file" />
<input type="submit" value="UPLOAD" />
</form>
<?php
}
else {
?>
if ($_FILES['userfile']['tmp_name'] == '') die ('No file submitted!');
$target = "uploaded/";
$target = $target . basename( $_FILES['userfile']['name']) ;
$ok=1;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['userfile']['name']). " has been uploaded. Parsing will start soon. ";
}
else {
echo "Sorry, there was a problem uploading your file.<br>";
}
// now do what you need with your file in $_FILES['userfile']['tmp_name']
Well, i could get what i wanted by using PHP Jasper XML. It is opensource too.
Well jrxml is created with ireports and its executed with java, you need a server like tomcat. look for information of jru-php it will help you

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