...

View Full Version : File Upload Script



jonnybinthemix
05-08-2006, 10:24 PM
I have an upload script, (below), and i thought that it would upload the same file to two directories...

Here goes, this is what i have:

Code:



<?php
$uploaddir="gallery";
$uploaddirl="largegallery";
if (is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);

move_uploaded_file($_FILES['file']['tmp_name'],$uploaddirl.'/'.$_FILES['file']['name']);
}
print "Image Uploaded Successfully"
?>


my assumption was that if i add another var with another directory and then moved the file twice it would save the file to 2 directories??

After realising it doesent work, i thought maybe the tmp file is deleted as soon as its moved once? is there a way of saving it to 2 directories?

Another thing, Instead of displaying a bit of text when the image is uploaded, i would like it to link the user to the album they have uploaded the image to?

Thanks again

Jon

Beagle
05-08-2006, 10:35 PM
mv, or move, takes a file from it's current location and moves it to the destination. It does not copy it.

What you need is to copy the file.

http://us2.php.net/manual/en/function.copy.php

You can use this on the destination file after your first move.

litebearer
05-08-2006, 10:54 PM
// Simple copy for a file
if (is_file($source)) {
copy($source, $dest);
}

jonnybinthemix
05-08-2006, 11:30 PM
hi guys, and thanks so much for you help...

i have had a look at the page and the code very kindly writen by you litebearer and i have come up with the following, however, it doesent work :(


<?php

$uploaddir="gallery";
$dest="largegalley";
$source="$_FILES['file']['tmp_name']"

if (is_uploaded_file($_FILES['file']['tmp_name']))

{
if (if (is_file($source)) {
copy($source, $dest);
}

move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}

print "Image Uploaded Successfully"

?>

I thought, (thinking i was being clever) that i would have to copy the file before i moved it, otherwise there would be nothing to copy! ;) so i thought i should make the source var to the temp path and the dest var to the destination directory.

However it looks like i was wrong, but am i on the right track??

Thanks

Jon

litebearer
05-09-2006, 08:17 AM
try this...


<?php
if ($_POST['upload']){
$uploaddir = "gallery/";
$snddir = "largegallery/";
$original_name = $_FILES['uploadfile']['name'];
$source = $uploaddir . $original_name;
$dest = $snddir . $original_name;
if(!move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploaddir . $_FILES['uploadfile']['name'])){
echo "Error in uploading file";
exit();
}else{
if(!copy($source,$dest)) {
echo "Error in copying file";
exit();
}
}
}else{
?>
<form enctype="multipart/form-data" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="uploadfile" type="file"><br>
<input name= "upload" type="submit" value="Upload File">
</form>
<?php
}
?>
Lite...

jonnybinthemix
05-09-2006, 12:03 PM
hi mate,

I have been trying to implement your code into my script but for various reasons im sure, i cant seem to get it working.

Would you be able to explain your code, because im sure i understand most of it, but im not sure on a couple of things.

I tried the following, amongst many other variations, i tried your code complete and it doesent work for me, so i was thinking i could just take the theory of your code and implement it into mine and maybe with a bit of luck i could get it working :(
however, heres my attempt!


<?php

$uploaddir="gallery/";
$snddir="largegallery/";
$original_name="$_FILES['uploadfile']['name']";
$source="$uploaddir . $original_name";
$dest="snddir . $original_name";

if( is_uploaded_file($_FILES['uploadfile']['tmp_name']));
{

move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploaddir . $_FILES['uploadfile']['name']);

copy( $source, $dest);
}

print " Image $original_name uploaded to the Random Images Gallery.";

?>

i think i can see why it doesent work, and this is probably why you have the if() statement at the top, because the $original_name var cannot be set first because the path doesent exist until the move has been carried out!? just a hunch, so i tried using your if() statement at the top, but i don understand fully so im at a bit of a loss, if you could explain how it works, i will have moe chance of getting something working

thanks :)

Jon

jonnybinthemix
05-09-2006, 05:05 PM
any takers? :D

litebearer
05-10-2006, 12:01 AM
Ok, let us review step by step.

1. In order to upload a file you need 2 basic parts

(A) a form for the user to identify the file to upload

(B) a bit of code to process the form

2. The above may be done using 2 separate files (a form file and a process file) OR as in our example it may be accomplished by merging the two processes into one file.

3. The file needs to know which of the two processes it is supposed do at any given time, hence the IF. Essentially, the IF says "IF the user has filled out the form and pressed the submit, then process the form data; OTHERWISE show the form for the user to complete.

4. The way you have your script setup, there needs to be two folders just beneath the folder that contains your script. Both of the folders need to be 'writeable'.

5. Another issue you have is using quotes inappropriately. do this to your code:

change these two lines from this



$original_name="$_FILES['uploadfile']['name']";
$source="$uploaddir . $original_name";


to this:



$original_name=$_FILES['uploadfile']['name'];
$source=$uploaddir . $original_name;


and change this line from this:



$dest="snddir . $original_name";


to this:



$dest=$snddir . $original_name;


6. Check to make sure that the name you assigned to the file in your form is the same as you use in processing - in my example I use uploadfile as the name.

See it that helps.

Lite...

jonnybinthemix
05-10-2006, 02:27 AM
Huraaaay!! lol

Took me long enough to get there!! lol

Worked absolutley diamond :)

Got me thinking now, of how i can improve :D lol

Thanks for your help :)

Jon

jonnybinthemix
05-10-2006, 02:35 AM
<?php

$uploaddir="gallery/";
$snddir="largegallery/";
$original_name=$_FILES['file']['name'];
$source=$uploaddir . $original_name;
$dest=$snddir . $original_name;

if( is_uploaded_file($_FILES['file']['tmp_name']));

{ move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $_FILES['file']['name']);
copy($source, $dest);
}
print " Image $original_name uploaded to the Random Images Gallery.";
?>

I have this, which works fantastic :D

However, typical me, enough is never enough! :)

I have tried putting some html at the end but as i thought it dident work, am i able to close the php and use html instead of the print() function at the bottom?

I would like a message to pop up as it does, saying image.jpg has successfully uploaded to the random image gallery, but i want to be able to format the font and then have something saying please wait wait while we redirect you to the Random Gallery, then maybe it can wait 4 seconds or something and then redirect the user to the gallery.

Is this possible, or am i aiming too high?

thanks

Jon

litebearer
05-10-2006, 03:47 AM
First, on this line


if( is_uploaded_file($_FILES['file']['tmp_name']));

get rid of the semi-colon at the end of the line

Next. to redirect, simply change this


print " Image $original_name uploaded to the Random Images Gallery.";
?>
to this

print " Image $original_name uploaded to the Random Images Gallery.";
echo "<B><I>please wait while we redirect you to the Random Gallery</i></b>";

?>
<META http-equiv="refresh" content="2; URL=new-page-blah-blah">

the 2 represents 2 seconds, change to suit your needs

jonnybinthemix
05-10-2006, 12:04 PM
hi mate i have this:



print "Image $original_name uploaded to the Random Images Gallery.";
echo "<br><br><B><I>Please wait while we redirect you to the Random Gallery</i></b>";

?>
<META http-equiv="refresh" content="4; URL="http://www.fartingfish.com/random.php">


It refreshes every 4 seconds, but refreshes to the same page, not to the link

any ideas?

jon

litebearer
05-10-2006, 01:04 PM
what is the name of the script file AND what is the name of the file to which you want the user sent?

jonnybinthemix
05-10-2006, 02:34 PM
the script is called upload.php and i want the user to be redirected to the random.php page

:D

litebearer
05-10-2006, 03:55 PM
Your quotes are off.

change this

<META http-equiv="refresh" content="4; URL="http://www.fartingfish.com/random.php">

to this

<META http-equiv="refresh" content="4; URL=http://www.fartingfish.com/random.php">

jonnybinthemix
05-10-2006, 04:49 PM
nice one mate,

works brill! :D

thanks again

jon



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum