Re: Upload a file
| From: | Shane McBride | Date: | Fri, 13 Oct 2000 00:07:42 +0000 |
| Subject: | Re: Upload a file | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19920@lists.php.net to get a copy of this message | ||
This actually requires a few steps. Here's a piece of sample code that will
give you the text box, and a "browse" button next to it. This file would be
called add_record.html.
Step #1:
<tr>
<td valign=top width="112">
<div align="right">Image File:</div>
</td>
<td valign colspan="3">
<INPUT type="file" name="picture" size=30>
</td>
</tr>
Use the above code within a form tag like this:
<FORM method="post" ENCTYPE="multipart/form-data"
action="add_record2.php">
Here's an example of it all put together:
<HTML>
<HEAD>
<TITLE>Sample file upload PHP file</TITLE>
</HEAD>
<BODY>
<FORM method="post" ENCTYPE="multipart/form-data"
action="add_record2.php">
<INPUT type="file" name="picture" size=30>
</FORM>
</BODY>
</HTML>
Step #2:
This code is within the add_record.php file that is referenced within the
form from above. This php file moves to the path you specify. The important
thing to remember here is that we are not talking about the http path, but
the true path on the server. An invalid path would be (in most cases):
exec("mv $picture www.yourdomain.com/images/$image");. This will surely make
the process barf.
<?
// Creates the image file with a timestamp
$timestamp = time();
$image= $timestamp.$picture_name;
exec("mv $picture /path/to/my/image/directory/$image");
echo "<img src=/URL/to/my/image/directory/$image";
?>
I created a timestamp for the image in case another image was uploaded with
the same name. It takes the file name and adds the system time in front of
it. If this precaution is not necessary, just use:
<? exec("mv $picture /pat/to/my/image/directory/$picture"); ?>
That's all there is to it. It works perfectly for me every time! I am VERY
new to PHP, so there may be a better way, but this is pretty
straightforward.
Shane
----- Original Message -----
From: "Liam Gibbs" <lgibbs@microlegend.com>
To: <php-general@lists.php.net>
Sent: Thursday, October 12, 2000 8:30 PM
Subject: [PHP] Upload a file
> I need to make a form to upload a file. Now, I have my text box (for the
> filename on the uploading computer) and the button that says go. When the
> user clicks the button, the file specified in the text box should be
> uploaded to the server. Anyone know how to possibly do this?
>
> Liam Gibbs
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
----- Original Message -----
From: "Liam Gibbs" <lgibbs@microlegend.com>
To: <php-general@lists.php.net>
Sent: Thursday, October 12, 2000 8:30 PM
Subject: [PHP] Upload a file
> I need to make a form to upload a file. Now, I have my text box (for the
> filename on the uploading computer) and the button that says go. When the
> user clicks the button, the file specified in the text box should be
> uploaded to the server. Anyone know how to possibly do this?
>
> Liam Gibbs
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>