Get full path of selected file in PHP - 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

Related

Have file automatically upload to server when .php is launched

I'm trying to write code in php so that when the .php file is opened, it automatically uploads from a specific file address on my windows computer to the localhost server.
This is my attempt but I'm not sure I fully understand how to do this without using an HTML form where the user specifies the file they want to upload.
<?php
$target = 'UPLOADED_FILE.csv';
move_uploaded_file('C:\Users\Ken.Feier\Desktop\temp\REPORT.csv', $target);
?>
I want the code to take the REPORT.csv file from my personal computer and upload it to our server with the file name UPLOADED_FILE.csv
UPDATE: I see that my problem will not be solved with php. Can anyone recommend any other solution involving Filezilla or any other FTP that can be automated?
That's not how it should be done.
You need a page with a html form, which will send the data to server on submit. Note that the file could be stored on your personal.
Form code e.g.
<form method="post" action="destination.php" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" value="upload" />
</form>
Then, on the server, you can use the $_FILES['filename'] which contains your file's infos. Note that when a file is uploaded to the server, it's stored in the tmp folder, which is temporary, so you have to move your file to a persistent directory with move_uploaded_file(); (move_uploaded_file Docs)
E.g:
<?php
$file = $_FILES['filename'];
move_uploaded_file($file['tmp_name'], '/new/destination/for/the/filename.php');

how to get image (file) by $_POST in mvc

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.

Uploading files with PHP & HTML

I'm having trouble with setting up the file upload to my php script.
Within the upload itself, I have it directed towards the path I want, but I'm not sure if I have the rest of the script syntactically right.
EDIT: I have resolved my situation with the file not properly uploading. I have edited the blocks of code to what I have currently working. If needed, please refer to the pre-edited version to see changes :).
Cheers.
$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
$uploadOk=1;
if(isset($_FILES["uploadFile"])){
$tardir= $tardir . "/" . basename($_FILES["uploadFile"]["name"]);
if(move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $tardir)){
echo "The file: ". basename($_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
The $targetnameis set to whatever the user inputs in the field. In this case, I have it as their email.
$targetname = filter_input(INPUT_POST, 'email');
This is what the <html> for the file upload looks like:
<form action= "userlogin.php" method= "post" enctype= "multipart/form-data">
<p><strong>Please choose a file to upload: <input type="file" name="uploadFile"><br></p>
<input type="submit" value="Upload File">
It's hard to guess all your code, as it seems you only have a snippet here, but I'm gonna take a wild guess. If target name comes from a text input like you say and you have it set to e-mail, are you leaving off a slash here?
$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
This would be like:
C:/xampp/htdocs/uploaddir/me#gmail.com
This runs:
$tardir= $tardir . basename($_FILES["uploadFile"]["name"]);
then:
C:/xampp/htdocs/uploaddir/me#gmail.comfilename
shouldn't it be:
C:/xampp/htdocs/uploaddir/me#gmail.com/filename
you can also comment out all your code here and var_dump() your file array, and echo out your paths before you try using them to see what data is held in each of your containers. If I get a bug like this I dump everything and echo every string until I find it..

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.

jquery PHP image upload (using .post method)

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

Categories