i would like to ask, how to get file(image) from my form.php ,then move file in another folder and get path of picture in Controller.php.
I am not sure how to get that file from post.
form.php
<form method="post">
<input name="image" type="file"><br>
<input type="submit" value="Uložit článok" />
</form>
Controller.php
$folder_path "images/" . $_FILES["image"]["name"]
$folder = "images/";
move_uploaded_file($_FILES["image"]["tmp_name"], "$folder" . $_FILES["image"]["name"]);
$Manager->AddPhoto($folder); //this is just adding into database
Add enctype="multipart/form-data" in your form tag. which MVC framework you are using? check the link
It's almost done. The best is decide the final destination by your own.
Let's suppose you have a folder called myPhotos. And the destination file is the datetime or timestamp that you can obtain with microtime(), also you can get the extension of the file with pathinfo. After all the destination files will be:
myphotos/123456myfile.jpg
Having this path, you can use this for move_uploaded_file and later for store in database.
Related
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
I understand the include function in php where the directory has been determined, like this:
include (dirname (__FILE__). '/file.php');
my question: can I use this function if the file from submit form?
my source code:
upload.php
<form action="process.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
process.php
<?php
include (dirname (__FILE__). '??????????');
?>
Bad english, sorry
If you really want to do this, you can do:
include $_FILES['file']['tmp_name'];
Uploaded files are not put in the same directory as the script, so you don't need dirname(__FILE). The tmp_name field contains the full pathname to the uploaded file.
One does not simply run a file uploaded by a user.
That being said, the details of the uploaded files are stored in the $_FILES array. The temporary name (and path) of the uploaded file is $_FILES['file']['tmp_name'].
You could either include this file directly:
include $_FILES['file']['tmp_name'];
Or, you could store this file first, using move_uploaded_file and then include that file:
$destination = "path/to/file.php";
move_uploaded_file($_FILES["file"]["tmp_name"], $destination);
include $destination;
Good luck!
I've got the following code to upload a file and add it to the folder called images located in the root of the server.
$file = $_FILES['prodImg']['tmp_name'];
$newLoc="/images/" . $_FILES['prodImg']['name'];
if(move_uploaded_file($file, $newLoc)){
//do some other code here
}
else{
echo 'error';
}
the form has this button to add the image
<input type="file" name="prodImg" id="prodImg" accept="image/png" />
the images folder has all permissions set to read, write and execute
every time i try to upload an image it go to the else statement.
not sure what i'm doing wrong here.how do i make it work properly?
Do you have attribute in your form?
enctype="multipart/form-data"
I.e.:
<form action="..." method="post" enctype="multipart/form-data">
Then you can try to check permission for that folder.
And also try change filepath:
$newLoc="./images/" . $_FILES['prodImg']['name'];
or
$newLoc="images/" . $_FILES['prodImg']['name'];
The path of your $newLoc variable may be wrong. Try ./images/.
The one specified may define an image folder on the same level as the home, root... folders if you use Linux.
remove the slash ahead of images
$newLoc= "images/".$_FILES['prodImg']['name'];
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']);
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.