...

View Full Version : Image upload... good code example



scroots
03-04-2003, 10:34 PM
does anoyone here have some good code for uploading files to a server as I have search and found many confusing ones. So does have any commented code? whcih they can post so i can learn from



scroots

x_goose_x
03-05-2003, 05:56 AM
<html>
<body>
<?php

$file_dir = "c:\www\uploads"; // directory where you want file to be uploaded to
$file_url = "http://localhost/uploads"; //same directory, but as link

foreach( $HTTP_POST_FILES as $file_name => $file_array ) {
if (substr($file_array['type'], 0, 5)=="image") {
echo "path: ".$file_array['tmp_name']."<br>\n";
echo "name: ".$file_array['name']."<br>\n";
echo "type: ".$file_array['type']."<br>\n";
echo "size: ".$file_array['size']."<br>\n";
if ( is_uploaded_file( $file_array['tmp_name'] ) ) {
move_uploaded_file( $file_array['tmp_name'], "$file_dir/".$file_array['name']);
echo "<a href=\"$file_url/$file_name\">".$file_array['name']."</a>\n\n";
}
} else {
echo "File is not an image.";
}
}
?>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000000000">
<input name="fupload" type="file"><br>
<input type="submit" value="Send File">
</form>
</body>
</html>

scroots
03-05-2003, 07:49 PM
thanks so far, but

i get a parse error for the following line:
$file_dir_=_"c:wwwuploads";_// directory where you want file to be uploaded to

yet i have a directory on my C: called wwwuploads

does anyone know the solution? do you not need to place slashes in directory paths?

Nightfire
03-05-2003, 09:24 PM
It should be


$file_dir = "c:\\wwwuploads"; // directory where you want file to be uploaded to

not


$file_dir_=_"c:wwwuploads";_// directory where you want file to be uploaded to

scroots
03-07-2003, 09:31 PM
it still doesn`t seem to work on my PC running apache.

scroots

Golden_Eagle
03-07-2003, 09:53 PM
If I was to use this with .ZIP files could I get away with changing this part


if (substr($file_array['type'], 0, 5)=="image") {


To this


if (substr($file_array['type'], 0, 5)==".zip") {


If not how would I do it?

x_goose_x
03-08-2003, 02:39 AM
For zips try:



$ftype = $HTTP_POST_FILES['fupload']['name'];
$ftype = substr($ftype, strlen($ftype)-4, strlen($ftype));
if ( $ftype == ".zip" ) {

x_goose_x
03-08-2003, 02:40 AM
Originally posted by scroots
it still doesn`t seem to work on my PC running apache.

scroots

Do you have PHP installed? Do other PHP scripts work?

scroots
03-08-2003, 01:08 PM
I have php installed and enabled as the hello world script works


scroots

scroots
03-08-2003, 01:12 PM
my current code is:


<html>

<body>

<?php

$file_dir_=_"C:\phpdev\www\upload\";_// directory where you want file to be uploaded to

$file_url_=_"http://localhost:5632/uploads";_//same directory, but as link



foreach(_$HTTP_POST_FILES_as_$file_name_=>_$file_array_)_{

____if_(substr($file_array['type'],_0,_5)=="image")_{

________echo_"path: ".$file_array['tmp_name']."<br>\n";

________echo_"name: ".$file_array['name']."<br>\n";

________echo_"type: ".$file_array['type']."<br>\n";

________echo_"size: ".$file_array['size']."<br>\n";

________if_(_is_uploaded_file(_$file_array['tmp_name']_)_)_{

____________move_uploaded_file(_$file_array['tmp_name'],_"$file_dir/".$file_array['name']);

____________echo_"<a_href=\"$file_url/$file_name\">".$file_array['name']."</a>\n\n";

________}

____}_else_{

________echo_"File is not an image.";

____}

}

?>

<form enctype="multipart/form-data" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="3000000000000">

<input name="fupload" type="file"><br>

<input type="submit" value="Send File">

</form>

</body>

</html>


can anyone help?

scroots

scroots
03-08-2003, 01:13 PM
how come loads of ______ appear in my code on this board? is it because i use editpad light?

scroots

redhead
03-08-2003, 01:21 PM
hmmm... you might want to read this tutorial:
http://www.developer.com/lang/php/article.php/1456041

scroots
03-08-2003, 01:36 PM
redhead thanks for the tutorial i will have a read and a fiddle.

scroots



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum