You are here: Re: Uploading a file and $_FILES « PHP SQL « IT news, forums, messages
Re: Uploading a file and $_FILES

Posted by B.r.K.o.N.j.A on 08/11/06 14:08

mpar612@gmail.com wrote:
> Hello,
>
> I am a newbie to PHP, MySQL. I am trying to create a basic file upload
> form. I want to get that working and then I want to integrate that
> into a form that will rename the file and save it to a directory and
> store the path to the file in the db, in addition to storing other text
> from other fields in the form. Then I will get that path using PHP to
> display the image file in a browser.
>
> First things first, I'm having difficulty getting the basic file upload
> form working.
>
> Here is the code I am using:
>
> <form enctype="multipart/form-data" action="<?php print
> $_SERVER['PHP_SELF']; ?>" method="post">
> <input type="hidden" name="MAX_FILE_SIZE" value="50000">
> Select a file: <input name="upfile" type="file">
> <input type="submit" value="Upload">
> </form>
>
> <?php
> $uploaddir = "uploads/";
> $uploadfile = $uploaddir . $_FILES['upfile']['name'];
> if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
> move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
> print("File upload was successful");
> } else {
> print("File upload failed");
> }
> print_r($_FILES);
> ?>
>
> When I load the page the following text is always displayed on the
> page:
> "File upload failedArray ( )"
>
> When I upload a file I recieve the following text on the page:
> "File upload was successfulArray ( [upfile] => Array ( [name] =>
> testing.php [type] => application/octet-stream [tmp_name] =>
> /tmp/phpVtNHIr [error] => 0 [size] => 574 ) )"
>
> I think my issue is with the $_FILES. Do I have to change the name and
> tmp_name values? The documentation is sort of vague to me, but then
> again I am pretty new to this. I know there are security issues and
> there is a lot more that needs to be done, but I need to start with the
> basics here.
>
> Any input or advice that anyone can give would be greatly appreciated.
> Also, if there are any online references that you could provide would
> be great. Thanks in advance!
>

Jerry told you the reason, and here is (sort of) solution. Execute php
code only when page was POST requested (meaning that the form was submitted)
to do so simply wrap your php code in one more if...
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$uploaddir = "uploads/";
$uploadfile = $uploaddir . $_FILES['upfile']['name'];
if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
print("File upload was successful");
} else {
print("File upload failed");
}
print_r($_FILES);
}
?>

That should work... :)
--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация