Home > Archive > PHP Language > January 2006 > Upload file?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi,
I would like to enable customers to upload files. How do I do that? I found
following with
google:'http://www.tutorialized.com/tutorial/Simple-Form-Based-Image-Upload/792'
what seems to be nice, I included it like this in my code:
PHP:
if ($_FILES['imagefile']['type'] == "image/gif" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/jpg" ||) { copy ($_FILES['imagefile']['tmp_name'], "files/" $_FILES['imagefile']['name']) or die ("<b>Could not copy</b>"); echo ""; echo "Name: ".$_FILES['imagefile']['name']."<br>\n"; echo "Size: ".$_FILES['imagefile']['size']."<br>\n"; echo "Type: ".$_FILES['imagefile']['type']."<br>\n"; echo "Copy Done....<br>\n"; } else { echo "<b>Couldn't upload your image, wrong filetype!<b><br>\n"; }
while my html looks like:
[html]
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" onsubmit=\"return
chkform()\" name=\"form\">
....
....
<td><p><input name="imagefile" type="file" size="15" maxlength="100000"
accept="text/*"></p></td>
....
....
</form>
[/html]
but it seems not to work, I always get 'Couldn't upload your image, wrong
filetype!'...Why? I tried to upload jpeg and gif image. Thank you!
--
chEErs roN
I'm root, I'm allowed to do this! ;)
keep on rockin'
| |
|
| roN wrote:
> Hi,
>
> I would like to enable customers to upload files. How do I do that? I
> found following with
>
google:'http://www.tutorialized.com/tutorial/Simple-Form-Based-Image-Upload/792'
> what seems to be nice, I included it like this in my code:
> PHP:
> if ($_FILES['imagefile']['type'] == "image/gif" || > $_FILES['imagefile']['type'] == "image/jpeg" || > $_FILES['imagefile']['type'] == "image/jpg" ||) > { > copy ($_FILES['imagefile']['tmp_name'], "files/" > $_FILES['imagefile']['name']) > or die ("<b>Could not copy</b>"); > echo ""; > echo "Name: ".$_FILES['imagefile']['name']."<br>\n"; > echo "Size: ".$_FILES['imagefile']['size']."<br>\n"; > echo "Type: ".$_FILES['imagefile']['type']."<br>\n"; > echo "Copy Done....<br>\n"; > } > else > { > echo "<b>Couldn't upload your image, wrong filetype!<b><br>\n"; > } >
> while my html looks like:
> [html]
> <form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\"
> onsubmit=\"return chkform()\" name=\"form\">
> ...
> ...
> <td><p><input name="imagefile" type="file" size="15" maxlength="100000"
> accept="text/*"></p></td>
> ...
> ...
> </form>
> [/html]
> but it seems not to work, I always get 'Couldn't upload your image, wrong
> filetype!'...Why? I tried to upload jpeg and gif image. Thank you!
hm and there's nothing passed when I'm gonna check like:
echo $HTTP_POST_FILES ['imagefile']['name']."<br>\n";
here's just nothing, why not?
--
chEErs roN
I'm root, I'm allowed to do this! ;)
keep on rockin'
| |
| Danny Wong 2006-01-27, 7:56 am |
| Hi,
You must use the following in your <form ...> tag
enctype="multipart/form-data"
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" onsubmit=\"return
chkform()\" name=\"form\" enctype="multipart/form-data">
This is used to tell your browser the POST include image file not only
text. Hopes this help.
Danny Wong
"roN" <NOspam@example.com> ???????:43t3grF1oholrU5@individual.net...
> roN wrote:
>
> google:'http://www.tutorialized.com/tutorial/Simple-Form-Based-Image-Upload/792'
>
> hm and there's nothing passed when I'm gonna check like:
> echo $HTTP_POST_FILES ['imagefile']['name']."<br>\n";
> here's just nothing, why not?
>
> --
> chEErs roN
>
> I'm root, I'm allowed to do this! ;)
> keep on rockin'
| |
| Ken Chau 2006-01-27, 6:56 pm |
| To summarize, you need a form tag like so:
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="file" name="uploadfile">
AND in your upload php script you should not just use "copy()"... in
case the file was injected to your server, check documentation on:
is_uploaded_file and move_uploaded_file
| |
|
| Ken Chau wrote:
> To summarize, you need a form tag like so:
>
> <form enctype="multipart/form-data" method="post" action="upload.php">
> <input type="file" name="uploadfile">
>
> AND in your upload php script you should not just use "copy()"... in
> case the file was injected to your server, check documentation on:
>
> is_uploaded_file and move_uploaded_file
yup, thank you guys, erm how can I return the whole image-path? like exactly
the String that was entered in the input field?
or let's say it like that:
I get all the POST data from that form, I'm gonna print these so the
customer can verify all the data, then he's gonna press another button to
add these data to the DB or he can go back and correct the data, I'm now
doin it like:
PHP:
/*** [Show submitted values, so the user can check them back before they're gonna be added to the DB] ***/ echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" name=\"check\">"; echo "the values you're going to submit are following:<br>\n"; echo "Systemname: ".$HTTP_POST_VARS['systemname']."<input type=\"hidden\" name=\"systemname\" value=\"" $HTTP_POST_VARS['systemname']."\"><br>\n"; echo "Price: $".$HTTP_POST_VARS['price']."<input type=\"hidden\" name=\"price\" value=\"".$$HTTP_POST_VARS['price']."\"><br>\n"; echo "Monitorsize: ".$HTTP_POST_VARS['monitorsize']."\"<input type=\"hidden\" name=\"monitorsize\" value=\"" $$HTTP_POST_VARS['monitorsize']."\"><br>\n"; echo "Additional monitor: ".$HTTP_POST_VARS['addmonitor']."<input type=\"hidden\" name=\"addmonitor\" value=\"" $HTTP_POST_VARS['addmonitor']."\"><br>\n"; echo "Age: ".$HTTP_POST_VARS['age']." months<input type=\"hidden\" name=\"age\" value=\"".$HTTP_POST_VARS['age']."\"><br>\n"; echo "Condition: ".$HTTP_POST_VARS['condition']."<input type=\"hidden\" name=\"condition\" value=\"" $HTTP_POST_VARS['condition']."\"><br>\n"; echo "Picture: ".$HTTP_POST_FILES['imagefile']['name']."<input type=\"hidden\" name=\"file\" value=\"\"><br>\n"; echo "Your name: ".$HTTP_POST_VARS['name']."<input type=\"hidden\" name=\"name\" value=\"".$HTTP_POST_VARS['name']."\"><br>\n"; echo "Your email: ".$HTTP_POST_VARS['email']."<input type=\"hidden\" name=\"email\" value=\"".$HTTP_POST_VARS['email']."\"><br>\n"; /***************************/
So as you might see, there's nothing to pass on for the image, how do I do
that? I have no idea... Or does anybody know another approach? Thank you
for hints and suggestions!
--
chEErs roN
I'm root, I'm allowed to do this! ;)
keep on rockin'
| |
| Ken Chau 2006-01-31, 3:59 am |
| I've not heard of people doing what you're doing quite exactly - the
whole verification before uploading. It's an interesting idea... the
problem you have is that the path selected from the previous form will
not be preserved. You might as well make the upload happen on the
SECOND step. Or... you'll have to do this:
1. upload on the first step
2. save the information about the temp file path & name + the real name
of the file (put in form)
3. move the file from tmp location to a dedicated pool of your own temp
location
4. after the second step's submission, move the file to the real
location
a bit clumsy... i suppose you could come up with some other clever form
of upload interface (I sometimes use a popup window)... or you can be
clever and use an AJAX upload thing.
-----------
Ken Chau
http://www.gizzar.com
|
|
|
|
|