Page 1 of 1

images

Posted: Tue Apr 01, 2008 1:00 pm
by purplelotus
I'm trying to let users upload images and I have a bit of trouble. I keep getting following errors:

Warning: move_uploaded_file(C:/projects/pratice/WWW/ch07/images/) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\projects\pratice\WWW\ch07\check_image.php on line 17

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpD9.tmp' to 'C:/projects/pratice/WWW/ch07/images/' in C:\projects\pratice\WWW\ch07\check_image.php on line 17

I know it has something to do with my path but I can't figure out how to fix it. Here is my code:

Code: Select all

<?php
//connect to db
require_once('dbconnection.php');
 
//pull var from form
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_POST['image_filename'];
$today = date("Y-m-d");
 
//upload image and check for image type
//path not working
$imageDir = "C:/projects/pratice/WWW/ch07/images/";
$imageName = $imageDir . $image_tempname;
 
//check to see if file is valid upload file (uploaded by post)
if (move_uploaded_file($_FILES['image_filename']['tmp_name'], $imageName))
{
    //get info about the image being uploaded
    list ($width, $height, $type, $attr) = getimagesize($imageName);
    
    switch ($type)
    {
        case 1:
            $ext = ".gif";
            break;
            
        case 2:
            $ext = ".jpg";
            break;
            
        case 3:
            $ext = ".png";
            break;
            
        default:
            echo "Sorry, but the file you uploaded was not a GIF,
                  JPG, or PNG file <br />";
            echo "Please hit your browser's 'back' button and try again";
    }
    
    //insert info into image table
    $insert = "INSERT INTO images
                (image_caption, image_username, image_date)
               VALUES
                ('$image_caption', '$image_username', '$today')";
    $insertResults = mysql_query($insert) or die (mysql_error());
    
    $lastpicid = mysql_insert_id();
    
    $newfilename = $imageDir . $lastpicid . $ext;
    
    rename ($imageName, $newfilename);
}
?>
<html>
<head>
    <title>Here is your pic</title>
</head>
<body>
<h1>So how does it feel to be famous</h1><br /><br />
<p>Here is the picture you just uploaded to our servers:</p>
<img src = "images/<?php echo $lastpicid . $ext;?>" align="left">
<strong><?php echo $image_name;?></strong><br />
This image is a <?php echo $ext;?> image.<br />
It is <?php echo $width;?> pixels wide
and <?php echo $height;?> pixels high.<br />
It was uploaded on <?php echo $today;?>.
</body>
</html>

Re: images

Posted: Tue Apr 01, 2008 1:29 pm
by s.dot
Make sure the folder is writable by php.

Also, I've edited your post to reflect how we would like php code posted. :)

Re: images

Posted: Thu Apr 03, 2008 9:20 pm
by altoyes
hi scottay and purplelotus:

i like the look of this code
it is what i have been looking for
it looks like it needs a form for uploading in there somewhere

Purplelotus:
could you share this complete code?
including the
images.sql

or post a link to the download url?

thankyou for your time

alto


i have just corrected this code
i have deleted a new line after

Code: Select all

echo "Sorry, but the file you uploaded was not a GIF,

Code: Select all

<?php
//connect to db
require_once('dbconnection.php');
 
//pull var from form
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_POST['image_filename'];
$today = date("Y-m-d");
 
//upload image and check for image type
//path not working
$imageDir = "C:/projects/pratice/WWW/ch07/images/";
$imageName = $imageDir . $image_tempname;
 
//check to see if file is valid upload file (uploaded by post)
if (move_uploaded_file($_FILES['image_filename']['tmp_name'], $imageName))
{
    //get info about the image being uploaded
    list ($width, $height, $type, $attr) = getimagesize($imageName);
   
    switch ($type)
    {
        case 1:
            $ext = ".gif";
            break;
           
        case 2:
            $ext = ".jpg";
            break;
           
        case 3:
            $ext = ".png";
            break;
           
        default:
            echo "Sorry, but the file you uploaded was not a GIF, JPG, or PNG file <br />";
            echo "Please hit your browser's 'back' button and try again";
    }
   
    //insert info into image table
    $insert = "INSERT INTO images
                (image_caption, image_username, image_date)
               VALUES
                ('$image_caption', '$image_username', '$today')";
    $insertResults = mysql_query($insert) or die (mysql_error());
   
    $lastpicid = mysql_insert_id();
   
    $newfilename = $imageDir . $lastpicid . $ext;
   
    rename ($imageName, $newfilename);
}
?>
<html>
<head>
    <title>Here is your pic</title>
</head>
<body>
<h1>So how does it feel to be famous</h1><br /><br />
<p>Here is the picture you just uploaded to our servers:</p>
<img src = "images/<?php echo $lastpicid . $ext;?>" align="left">
<strong><?php echo $image_name;?></strong><br />
This image is a <?php echo $ext;?> image.<br />
It is <?php echo $width;?> pixels wide
and <?php echo $height;?> pixels high.<br />
It was uploaded on <?php echo $today;?>.
</body>
</html>