Page 1 of 1

upload and move (move_uploaded_file perhaps?)

Posted: Thu Sep 29, 2005 5:40 am
by thurstan
Hello.

I have an image upload script, as such:

Code: Select all

<?

$FILENAME=date("mdyHis");
$RESIZEWIDTH=500;
$RESIZEHEIGHT=500;

include("function.php");

if($_FILES['image']['size']){
	if($_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){
		$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){
		$im = imagecreatefrompng($_FILES['image']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/gif"){
		$im = imagecreatefromgif($_FILES['image']['tmp_name']);
	}
	if($im){
		if(file_exists("$FILENAME.jpg")){
			unlink("$FILENAME.jpg");
		}
    	ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
		
	ImageDestroy ($im);

}
}


?>
<html>
<head>
<link href="styles.css" rel="stylesheet" type="text/css">
<title>Picture Upload</title>
</head>
<body>
<h1>Picture Upload</h1>

<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"
<?php 
list($width, $height, $type, $attr) = getimagesize($FILENAME.".jpg");
echo"$attr"; ?> 
alt="Last image uploaded..." />
<form enctype="multipart/form-data" method="post">
<h3>Resize Image &raquo; 500px wide</h3>
<input type="file" name="image" size="50"><br><br>
<input type="submit" value="Upload Photo">
</form> 

</body>
</html>
Once the image is uploaded, I want to move the uploaded image to a specified file. For this I have this little bit of code:

Code: Select all

<?php
if($file_name !="")
{
//alter the path below when stuff is moved over
copy ("$file", "/path_to_folder/$file_name")
         or die("Could not copy file");
}
else { die("No file specified"); }
?>
My question is, where do I insert this bit of code to do the moving? Does it fir into the main code, do I have to create a separate file to contain the moving code? I've tried "move_uploaded_file function", but again, do not know ehere to insert this.

Any help would be much appreciated!

Thanks

Posted: Thu Sep 29, 2005 5:44 am
by Jenk
Have a read about this little function: move_uploaded_file()

:)

Posted: Fri Sep 30, 2005 3:53 am
by thurstan
thanks, i had a read of that, and i've added a form action to the upload script that actions this page (uploader.php):

Code: Select all

<?php
    if (move_uploaded_file($_FILES['image']['tmp_name'], "/full/path/to/gfx/product_images/")) {
        print "Received {$_FILES['image']['name']} - its size is {$_FILES['image']['size']}";
    } else {
        print "Upload failed!";
    }
?>
I've tried many things, but I it always fails, I get this error message:

Warning: move_uploaded_file(/path/): failed to open stream: Is a directory in /path/uploader.php on line 2


Any ideas?

Thanks v.much

Posted: Fri Sep 30, 2005 4:06 am
by Jenk
You need to specify the filename as well, so if for example, the file you are uploading will be "image.gif", then the function should be as so:

Code: Select all

<?php

move_uploaded_file($_FILES['userfile']['tmp_name'], "/path/to/folder/image.gif") or die('Couldn't move file');

?>
:)

Posted: Fri Sep 30, 2005 4:06 am
by n00b Saibot
change

Code: Select all

<?php
    if (move_uploaded_file($_FILES['image']['tmp_name'], "/full/path/to/gfx/product_images/")) {
to

Code: Select all

<?php
    if (move_uploaded_file($_FILES['image']['tmp_name'], "/full/path/to/gfx/product_images/".$_FILES['image']['name'])) {
edit:this is not fair....waah!

Posted: Fri Sep 30, 2005 4:20 am
by thurstan
thanks a lot guys.

n00b - that's the stuff - worked a treat! thanks so much.

Posted: Fri Sep 30, 2005 4:34 am
by n00b Saibot
thurstan wrote:thanks a lot guys.

n00b - that's the stuff - worked a treat! thanks so much.
No prob.

PS: and I take back my whining

Posted: Wed Oct 05, 2005 10:47 am
by thurstan
apologies for my bring ing this again, but can anyone tell me where this bit of code:

Code: Select all

rename($image.".jpg" , "/home/httpd/vhosts/eveco.co.uk/httpdocs/gfx/product_images/"".$image.".jpg");
would fit into this code:

Code: Select all

<?
$FILENAME=date("mdyHis");
//$FILENAME=uniqid(rand(), true);
$RESIZEWIDTH=500;
$RESIZEHEIGHT=500;

include("function.php");

if($_FILES['image']['size']){
	if($_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){
		$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){
		$im = imagecreatefrompng($_FILES['image']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/gif"){
		$im = imagecreatefromgif($_FILES['image']['tmp_name']);
	}
	if($im){
		if(file_exists("$FILENAME.jpg")){
			unlink("$FILENAME.jpg");
		}
    	ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
		
	

ImageDestroy ($im);
	
	}
}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>I-R</title>
</head>
<body>

<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"
<?php 
list($width, $height, $type, $attr) = getimagesize($FILENAME.".jpg");
echo"$attr"; ?> 
alt="Last image uploaded..." />


<form enctype="multipart/form-data" method="post">
<h3>Resize Image &raquo; 500px wide</h3>
<input type="file" name="image" size="50"><br><br>
<input type="submit" value="Upload Photo">
</form> 

</body>
</html>
in order for the script to resize and move the image to a specificed folder.....thanks