How to upload a file in PHP ?
Moderator: General Moderators
-
nomansoofi
- Forum Newbie
- Posts: 4
- Joined: Wed Jul 25, 2007 1:24 am
How to upload a file in PHP ?
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
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
- spamyboy
- Forum Contributor
- Posts: 266
- Joined: Sun Nov 06, 2005 11:29 am
- Location: Lithuania, vilnius
Temp file looks ~like this (in windows consonle):
Your php.ini file (by default) should be in folder:
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
C:\WINDOWS\TEMP\php642.tmpCode: Select all
[YOUR_DISK]:\WINDOWS\System32\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
feyd | Please use
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]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]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.
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
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>";
?>-
nomansoofi
- Forum Newbie
- Posts: 4
- Joined: Wed Jul 25, 2007 1:24 am
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Corrent me in my code please
you need to move the temporary file uploaded by php...nomansoofi wrote:CORRECT ME IN MY CODE IF I AM WRONG ANYWHERE .........
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)]
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
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
http://www.tizag.com/phpT/fileupload.php