How to upload a file in PHP ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nomansoofi
Forum Newbie
Posts: 4
Joined: Wed Jul 25, 2007 1:24 am

How to upload a file in PHP ?

Post by nomansoofi »

Hi,
i am a new comer in PHP and i have to upload a file in my php web form. I have read some tutorials in which they mentioned that when we go to upload a file in php it saves by default in a temporary directory and after that we move or copy that file into our mentioned directory or folder. The name of that temporary folder / directory can be change in php.ini file.

My question is that how to access to php.ini file and how do i change it? Please help me if anyone having command on php.

Thank you in adavnce,
Noman Soofi
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Temp file looks ~like this (in windows consonle):

Code: Select all

C:\WINDOWS\TEMP\php642.tmp
Your php.ini file (by default) should be in folder:

Code: Select all

[YOUR_DISK]:\WINDOWS\System32\
I just don't see any possible reason why should you need to change it.

And here is basic upload script that I wrote just to understand - how does it work.

Code: Select all

<ul>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <!-- here you set max file size (now 10000kb) -->
<li><input name="file_to_upload" type="file" /></li>
<li><input type="submit" value="Upload File" /></li>
</form>
<?php
$where_to_upload = "uploadsdir/"; // directory where uploadet while will go.
if(isset($_FILES['file_to_upload'])) {
$where_to_upload = $where_to_upload . basename( $_FILES['file_to_upload']['name']);
move_uploaded_file ($_FILES['file_to_upload']['tmp_name'], $where_to_upload);
echo "Where uploadet: ".$where_to_upload."<br />";
echo "TEMP file: ".$_FILES['file_to_upload']['tmp_name']."<br />";
echo "File uploadet secssesfully.";
} else {
echo "Pleas select file to upload.";
}
?>
</ul>
nomansoofi
Forum Newbie
Posts: 4
Joined: Wed Jul 25, 2007 1:24 am

Post by nomansoofi »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php 
$where_to_upload = "uploadsdir/"; // directory where uploadet while will go. 
if(isset($_FILES['file_to_upload'])) { 
$where_to_upload = $where_to_upload . basename( $_FILES['file_to_upload']['name']); 
move_uploaded_file ($_FILES['file_to_upload']['tmp_name'], $where_to_upload); 
echo "Where uploadet: ".$where_to_upload."<br />"; 
echo "TEMP file: ".$_FILES['file_to_upload']['tmp_name']."<br />"; 
echo "File uploadet secssesfully."; 
} else { 
echo "Pleas select file to upload."; 
} 
?>

IN YOUR ABOVE GIVEN SAMPLE CODE, CAN YOU TELL ME WHAT NAME WILL I PUT AT ... ['tmp_name'] ????


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Nothing. $_FILES['file_to_upload']['tmp_name'] is the name of the temporary file that PHP has stored. You need to move that file to whereever you want to store it. Look up move_uploaded_file().

http://www.ooer.com/index.php?section=php&id=21 might also help.
nomansoofi
Forum Newbie
Posts: 4
Joined: Wed Jul 25, 2007 1:24 am

Corrent me in my code please

Post by nomansoofi »

CORRECT ME IN MY CODE IF I AM WRONG ANYWHERE .........

Code: Select all

<html>
<body>
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="uploader.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>


</body>
</html>

<?php

$uploaddir = '/uploads/';
$uploadedfilename = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";


?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I take it you haven't actually tried it?
nomansoofi
Forum Newbie
Posts: 4
Joined: Wed Jul 25, 2007 1:24 am

Post by nomansoofi »

i have tried so many times this sample code for this task and m continousley trying but unsuccessfull so far
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

nomansoofi wrote:i have tried so many times this sample code for this task and m continousley trying but unsuccessfull so far
And where does it fail?
Jello
Forum Newbie
Posts: 16
Joined: Mon Jul 16, 2007 1:53 pm

Re: Corrent me in my code please

Post by Jello »

nomansoofi wrote:CORRECT ME IN MY CODE IF I AM WRONG ANYWHERE .........

move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)]
you need to move the temporary file uploaded by php...
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Here you are, take a look at this site and go over the code (even use it yourself and test it out - see how it works).

http://www.tizag.com/phpT/fileupload.php
Post Reply