Library tutorials & articles
Uploading files in PHP
- Introduction
- The Code
- Handling the upload
Introduction
Think of an application where you want your users to upload files to your machine. Uploading files in PHP is very easy. You can upload any type of file from client to server.
Files are uploaded from the browser using an <input> tag, with the type parameter in the <input> tag set to "File". This is supported by all popular browsers currently available on the market. The important thing is to set the ENCTYPE attribute of the form to "multipart/form-data" and set the form's action element to the file upload page. The file upload page will handle the file uploading.
The code is on the next page
Related articles
Related discussion
-
Ventrilo Server Status Php. Snmp Server Enable Traps
by MarkHewitt (0 replies)
-
Aion Server Status Php, Server Racks Cabinets
by MarkHewitt (0 replies)
-
Moparscape Server Status Php - Global Catalog Server Could Not Be Located
by MarkHewitt (0 replies)
-
accutane online without prescription, isotretinoin online, accutane cost, accutane acne treatment
by torgo (0 replies)
-
We are seeking a highly skilled iPhone Develope
by Kim abunado (0 replies)
Related podcasts
-
EarthClassMail.com - Moving from LAMP to .NET 3.5
Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.
Events coming up
-
Apr
17
WebTech Conference 2010 - Bulgaria
Veliko Turnovo, Bulgaria
6th edition of WebTech conference will be held. A 2 day conference about : - Web Technologies - Blogs and blogging - Web 3.0 - Open Web - Mobile technologies - Internet Business
I am trying to allow people to upload image files and post them to me.
I have copied your code exactly but get the following;
Parse error: parse error, unexpected T_IF in /nfs1/expatriatespain.com/www/test site/myupload.php on line 16
Any ideas?
Is there anyway to change the filename as it is uploaded? So myhouse.jpg is auto changed to file1.jpg
For instance, anytime a file is uploaded via the form... Have it name the file MYFILE.jpg
so that when a new file is uploaded it overwrites the old file without having to have the user specify the correct filename before uploading it.
gud day!
i have problems regarding the file i uploaded on the server.
the file is pic.bmp
i uploaded it in "subs/images"
then i am about to create an exec statement to convert this logo.
its permission is "-rw-------" and i would like to change it.
i typed "chmod 777 pic.bmp"
it says "operation not permitted"
what can i do?
Hi,
I'm relatively new to php. I'm using a code simliar to the one posted for file uploads. I was wondering if there was any way to set the permisson to "read only" upon upload. I've been having problems with uploading images that will only display if I manually set the permission in my site admin tool. Any help would be muck appreciated.
Thank you,
Mike
how can i get the exact address of the uploaded file i browsed.
like when i browsed it from "C:/picture.bmp"
how can i get this address? i mean how could i get these exact words?
and when i upload this picture to the server, i declared the address of the server.
but then it says, "permission denied"
what does it mean?
method=post change to method="post"
type=file change to type="file"
type=submit change to type="submit"
Also note - The php manual recommends you include max file size, which appearently warns the user before the upload begins.
btw the script worked fine for me after these corrections
my site: carterettechservice.com
The T_IF error is the missing ";" at the end of the $uploaddir = "/path" statement in his code. In my case the code simply isn't working and I'm not sure why. The file just doesn't upload.
The problem is a missing ";"
add a semi colon after:
$uploaddir = "/upload/"
i have done everything you have said and this error comes up and cant figure out what this means right now
Parse error: parse error, unexpected T_IF in c:\appserv\www\upload.php on line 7
Thanks for any reply
Are there C functions equivalent to those in php which will allow
me to write the upload.php in C? I have a lot of form processing
that is all written in C, and I would like to integrate the uploading
of files right into other procedures.
Thanks for you help.
John
This error message is common when the upload directory does not have the right permissions for the web server to write to it.
You must change the write permissions of the upload directory allowing the web server to write to it.
Steven
Steven
Nick
move_uploaded_file($_FILES['userfile']['tmp_name'], "home/successa/public_html/".$_FILES['userfile']['name']);
When I run it, I get:
Warning: move_uploaded_file(home/successa/public_html/NTUSER.DAT.LOG): failed to open stream: No such file or directory in /home/successa/public_html/processfile.php on line 15
Warning: move_uploaded_file(): Unable to move '/tmp/php6fQXnx' to 'home/successa/public_html/NTUSER.DAT.LOG' in /home/successa/public_html/processfile.php on line 15
I don't know how to specify the directory. When I log into my control panel, whatever root directory I am in, I see a public_html folder there. I've tried just specifying "/public_html" as the directory but that doesn't work either! I read someplace that it should be the "absolute location" but in this case I'm not sure what that means. I've checked for write permission and I do have that...??? I feel so dumb!
i
when execute that script the value of $FILES["userfile"]["tmpname"], $FILES["userfile"]["type"], and $FILES["userfile"]["size"] are empty,
the value of $_FILES["userfile"]["name"], yes, take the name of the file to upload, but, don't make nothing, why ?
thank's in advance
<?php
if(!empty($userfile))
{
copy($userfile, "kuva.gif");
echo("file uploaded");
}
elseif (empty($userfile))
{
echo("FAILED!");
}
?>
This is due to a change in the way PHP lets you access POST'ed variables. I've updated the article to reflect these changes.
Hello, I have tried using your script and I have checked over my code for syntax and it appears to be the exact same as yours. When I test it, it appears to not upload anything and skips right to the else statement "FAILED". It appears as username is not being passed properly to the php page. Also copy($userfile, "kuva.gif"); that line what should the value in " " be. THe path where the uploaded file is going to be stored? I dont understand how someone could have a .gif. Plz help
HTML PAGE
<html>
<form action="myupload.php" method=post enctype="multipart/form-data">
submit this file: <input type=file name="userfile"><br>
<input type=submit>
<br>
</form>
</html>
PHP
<html>
<?php
if(!empty($userfile)) {
copy($userfile, "kuva.gif");
echo("file uploaded");
}
else {
echo("FAILED!");
}
?>
</html>
Some more posts on this topic (comments page took me to forums
):
http://www.developerfusion.com/forums/topic.aspx?id=14842
http://www.developerfusion.com/forums/topic.aspx?id=13447
Perhaps it's more likely that the data getting put into $userfile is not working properly leaving it empty.
I'd also rather have "<?php" instead of just "<?" when starting to write php code.
<?php
if(!empty($userfile))
{
copy($userfile, "kuva.gif");
echo("file uploaded");
}
else
{
echo("FAILED!");
}
?>
"./phpIV1As" sounds like a directory name, probably the directory of your php folder or where your files are stored on your webspace, which may be inaccessable to you.
With your "c:pippo.ttt" file name, have you tried putting in the "\" backspace after C: in this text? If you don't, which is what you have done here, then PHP will look for the pippo.ttt file in the present working directory of the C: Drive, which is unnecessarily the root folder of the C: drive.
Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upl
bonk on head
Perhaps you don't have a www/phptest/ folder
The text "'/home/www/phptest/test.txt" was just an example, you can make it anything you want, as long as its a valid place in your webspace.
I have had this problem before. Your code appears to be ok so it seems odd that it is not working. Try using the same code but without the braces {} - this often helps me in PHP when for some reason the second branch of the conditional is never reached.
Hi!
I used this code for my upload form and worked ok...there's just something i can't get to work.
I placed a check for the "empty input field" and I can't get it to work.
<html>
<?
if(!empty($userfile)) {
copy($userfile, "kuva.gif");
echo("file uploaded");
}
else {
echo("FAILED!");
}
?>
</html>
No matter if I turn the code "upside down", it only does the "if" part..can you help me?
-meissi-
I am new php programmer but when I test your upload php. I got a message
Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upload.php on line 6
file up...
You need to make sure the directory you are uploading to has write permissions. If it doesn't you will get that error!
That's so easy in my self server, but I am using free hosting like iwebland.com.
May I change temporary directory? no I think so.
I tried do a copy inside my root, and I discover that I haven't permision to do that.
I don't know java.
I can't use ftp because I can't give the access to my customer, but he need put images in the database.
May redirect the upload trought "<input type=file..." to the directory in my root?
Hi!
I used this code for my upload form and worked ok...there's just something i can't get to work.
I placed a check for the "empty input field" and I can't get it to work.
<html>
<?
if(!empty($userfile)) {
copy($userfile, "kuva.gif");
echo("file uploaded");
}
else {
echo("FAILED!");
}
?>
</html>
No matter if I turn the code "upside down", it only does the "if" part..can you help me?
-meissi-
are you talking to me?
Hi
Run your application from the "public" folder under your server's root directory.
S.S
Try on a php supported server (i.e. sign up on www.free.fr)
bye
I am new php programmer but when I test your upload php. I got a message
Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upload.php on line 6
file uploaded
instead. what should I do to solve this prob?
I'm a new inexpert php programmer.
I've tested your script upload.html and upload.php and it is all ok.
Now, I like modify script to upload a specified file (i.e c:\pippo.ttt) from pc to server, without insert manually the file name in the input box, but I have some problems.
test 1)
I try to substitute in upload.htm the line:
:---------
submit this file: <input type=file name="userfile"><br>
:---------
with this line :
:---------
submit this file: <input type="hidden" name="userfile" value="c:pippo.ttt"><br>
:---------
... but the server ( http://infrarossi.free.fr/test/upload5.htm ) replay :
:---------
Warning: Unable to open 'c:pippo.ttt' for reading: No such file or directory in your script on line 12
:---------
--------------------------------------------------------------
test 2)
to debug I add a line in upload.php
:---------
echo "$userfile";
echo "<br>-xxx-<br>";
:---------
... but the server replay:
:---------
./phpIV1Asq
-xxx-
Warning: Unable to open 'c:pippo.ttt' for reading: No such file or directory in your script on line 9
:---------
-------------------------------------------------------------
The question:
Why the string c:pippo.ttt is echoed as ./phpIV1As ?
Thank you for your help.
Best regards
I'm a new inexpert php programmer.
I've tested your script upload.html and upload.php and it is all ok.
Now, I like modify script to upload a specified file (i.e c:\pippo.ttt) from pc to server, without insert manually the file name in the input box, but I have some problems.
test 1)
I try to substitute in upload.htm the line:
:---------
submit this file: <input type=file name="userfile"><br>
:---------
with this line :
:---------
submit this file: <input type="hidden" name="userfile" value="c
:---------
... but the server ( http://infrarossi.free.fr/test/upload5.htm ) replay :
:---------
Warning: Unable to open 'c
:---------
--------------------------------------------------------------
test 2)
to debug I add a line in upload.php
:---------
echo "$userfile";
echo "<br>-xxx-<br>";
:---------
... but the server replay:
:---------
./phpIV1Asq
-xxx-
Warning: Unable to open 'c
:---------
-------------------------------------------------------------
The question:
Why the string c
Thank you for your help.
Best regards
when a partner wrote a simple script to receive uploaded files, the owner of the file was set to nobody, which is the apache webserver, I believe.
In this case, we can't access or manipulate the uploaded files via ftp or the shell. Chmod doesn't seem to work.
Are we doing something wrong or is this always the case?
Thanks,
Collin
This thread is for discussions of Uploading files in PHP.