Uploading files with PHP & HTML - php

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..

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

How to Upload a File within HTML Context

I'm trying to get a basic PHP script up and running where a user uploads a file to the server, and the file is saved to a folder on the server. I am using a HTML form that looks like
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50000000" />
<br />
<input type="submit" value="Upload" />
</form>
and I have a PHP file saved as upload_file.php that looks like
<?php
$target = "upload/";
$target = $target . basename($_FILES['uploaded']['name']);
$ok = 1;
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file " . basename($_FILES['uploadedfile']['name']) . "
has been uploaded";
} else {
echo "Sorry, there was a problem uploading your file.";
}
?>
The issue is that after the file is uploaded, it simply shows the php code. Nothing else occurs, and no image is saved in uploads. I've looked at several tutorials, and none of them seem to mention this. I'm guessing it's glaringly obvious, but I could use some help. The error echo "Sorry... never occurs either.
Thanks.
Quick Note: I'm using Apache to host a local web server.
If it simply shows the php code, then PHP is not running. Verify tha PHP is installed and mod_php5 activated.
http://www.thesitewizard.com/php/install-php-5-apache-windows.shtml

fail in uploading file using html form

Failed to upload file using HTML form:
after I click the submit botton, I could print out the $_FILES["file"]["name"] and $_FILES["file"]["type"] and ["size"] and ["tmp_name"] and
["error"] and etc.but I could not find the file in the tmp folder(it should be there by default)!I don't know why.
the html code goes like this:
<form action="manage.php?act=getback.questionOpt" target="fileUp" method="post" id="f1" enctype="multipart/form-data">
<p id="file_u">
<input type="file" name="file">
<iframe width="0px" height="0px" name="fileUp" style="display:none">
</iframe>
</p>
</form>
Uploaded files are deleted from the temp-directory unless they are moved or renamed!
There is a small note about that just above the third example on the PHP-Documentation at http://de2.php.net/manual/en/features.file-upload.post-method.php
Therefore you will not be able to see the file after the request is finished.
Simply renaming the file should do the trick.
How big is your file? If it is too big and exceeds your server limit, it won't upload. If your server has uploading disabled by default, you will need to turn it on in php.ini. This is how:
Open the php.ini file in your editor.
Search for the text string file_uploads. You will find something which says file_uploads = Off | On. If it says Off as the value, problem solved. Otherwise, it could be commented (check for semicolon at start of line). Otherwise:
Change the file permissions of your tmp folder.
Unless you show us the full code, there is nothing else I can do to help you.

Using only PHP and HTML to act like JavaScript

I have an assignment for school, and I'm not sure how the teacher wants us to accomplish a task.
We need to get an uploaded file as a temp file only (index.php)
Output size of file (upload.php)
User can confirm save of file or not (upload.php)
So, I have the majority down, but my problem lies with creating the temp file into a permanent file.
index.php
<html>
<form action="http://mysite.org/~me/upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php
<?php
if (($_FILES["file"]["type"] == "application/vnd.ms-excel"))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
}
}
?>
<form action="" method="POST">
<input type="submit" value"YES please save">
<form>
<?php
if (isset($_POST['submit']))
{
//Code for saving file
echo 'File saved!';
}
?>
Is it possible to go about it this way? My last echo statement does not work, so I'm doubtful the file save would be as well.
Hopefully the following comments can help you with the part you are stuck on.
In case you hadn't realized it already, any files uploaded with PHP are deleted once the PHP request that handled the uploaded file terminates. This means, if you don't do anything with the temp file from the upload, it will be deleted when the PHP script terminates.
One function of interest to you will be move_uploaded_file() which will move the temporary file from the upload to a permanent location of your choice.
Since the file will be uploaded and then you have to display the size and ask the user to confirm the upload, you will have to move the temp file to a permanent temporary location where it is kept when the user hasn't confirmed they want to keep the upload.
I'm not sure if you have been introduced to sessions yet, but if not, you will probably need some hidden form element that will keep track of what file they uploaded, otherwise you can keep this info in the session.
Then when the person submits the form saying they want to keep the file, you can move it again to a permanent location, or if they say no, then delete the file. The problem is, if they never say yes or no, then the file remains on the system.
Hope that helps.
Yep, this should work. Your if statements will catch the form submission and then echo your string there. A few little errors in your markup:
<input type="submit" value"YES please save">
Should be
<input type="submit" value="YES please save" name="submit">
Your final if statement in PHP is looking for a post variable named 'submit' but your <input type="submit"> tag has no name.
The file is saved to a temporary location when the upload completes. You can access this temporary file with $_FILES['file]['tmp_name'] BUT the file will be removed at the end of the request if you do nothing about it. This means that when the user clicks YES please save button, the file will not be available any more.
This means that you have to save the file to a disk in the first place, when you first call the upload.php file. There is no way to keep the file "in memory" while the user decides whether or not to save the file permanently.

bool(false) Uploading a file

I have had problems with a simple php script in which I can upload a file to a certain folder. I have tried multiple ways in doing this and I still have not had success.
Any errors in my code or advice on how to correct the issue will be taken gracefully.
Main Php Code:
<p>Browse For a File on your computer to upload it!</p>
<form enctype="multipart/form-data" action="upload_photos.php" method="POST">
Choose Photo:
<input name="userfile" type="file" /><br />
<input type="submit" value="Upload Photo" />
<?PHP
if ($userfile_size>250000)
{$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";}
else{
if (!($userfile_type<>"image/jpeg" OR $userfile_type<>"image/tiff" OR $userfile_type<>"image/png"))
{$msg=$msg."Your uploaded file must be of JPG, PNG, or tiff. Other file types are not allowed<BR>";
$file_upload="false";}
}
?>
</form>
</label>
</form>
Php code that is called upon on click (upload_photos.php)
<?php
$target_path="uploads/";
chmod("uploads/", 0755);
$target_path=$target_path . basename( $_FILES['uploadedfile']['name']);
$test=move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
if($test) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
var_dump($test);
}
?>
I do not understand why my end results [upon clicking "Upload Files" Button] include only the following results:
"There was an error uploading the file, please try again!bool(false)"
One more thing: I have also tried using the full computer folder path for $target_path and chmod.
Does anybody see what I am doing wrong?
You have <input name="userfile" but then use $_FILES['uploadedfile'] in your script - use one or the other.
Other than that, make sure the chmod worked and the folder is writable.
bool(false) is the output of var_dump($test);, indicating that move_uploaded_file is returning false.
As a basic debugging step, you should try var_dump($_FILES) to make sure you're accessing the right element of that array (I can tell from your code that you aren't, the index will be the name attribute of your <input type="file"/> element).
You have at least one other serious flaw in your logic... The PHP code in your upload form doesn't make any sense. That block of PHP code will execute server-side before the user has ever uploaded a file. It can't possibly work. The two variables you're checking, $userfile_size and $userfile_type, are not defined anywhere.
In my case, I forgot to create a folder where I want to upload. So check once the specified upload path is available or not.

Categories