upload and move (move_uploaded_file perhaps?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

upload and move (move_uploaded_file perhaps?)

Post 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
Last edited by thurstan on Wed Oct 05, 2005 10:48 am, edited 2 times in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Have a read about this little function: move_uploaded_file()

:)
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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');

?>
:)
Last edited by Jenk on Fri Sep 30, 2005 4:07 am, edited 1 time in total.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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!
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

Post by thurstan »

thanks a lot guys.

n00b - that's the stuff - worked a treat! thanks so much.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

Post 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
Post Reply