Jump to content

upload image problem


garydt

Recommended Posts

I'm trying to let the user to upload an image and to store the image file in the directory called uploads and to store the link to the image in the database along with the user's username. I'm getting error-

Warning: unlink() [function.unlink]: Permission denied in C:\Program Files\xampp\htdocs\epeople\upim.php on line 81

 

Can you say i'm storing the image in the directory correctly along with what else i'm doing wrong?  I've been trying to do this all weekend.

The code-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?php

 

if ($_SERVER['REQUEST_METHOD'] == "POST")

{

 

    /* SUBMITTED INFORMATION - use what you need

    * temporary filename (pointer): $imgfile

    * original filename          : $imgfile_name

    * size of uploaded file      : $imgfile_size

    * mime-type of uploaded file  : $imgfile_type

    */

 

    /*== upload directory where the file will be stored

          relative to where script is run ==*/

   

    $uploaddir = "./uploads/";

   

 

 

 

    //-- RE-SIZING UPLOADED IMAGE

 

    /*== only resize if the image is larger than 250 x 200 ==*/

    $imgsize = GetImageSize($imgfile);

 

    /*== check size  0=width, 1=height ==*/

    if (($imgsize[0] > 250) || ($imgsize[1] > 200))

    {

        /*== temp image file -- use "tempnam()" to generate the temp

            file name. This is done so if multiple people access the

            script at once they won't ruin each other's temp file ==*/

        $tmpimg = tempnam("/tmp", "MKUP");

 

        /*== RESIZE PROCESS

            1. decompress jpeg image to pnm file (a raw image type)

            2. scale pnm image

            3. compress pnm file to jpeg image

        ==*/

       

        /*== Step 1: djpeg decompresses jpeg to pnm ==*/

        system("djpeg $imgfile >$tmpimg");

       

 

        /*== Steps 2&3: scale image using pnmscale and then

            pipe into cjpeg to output jpeg file ==*/

        system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

 

        /*== remove temp image ==*/

        unlink($tmpimg);

 

    }

 

    /*== setup final file location and name ==*/

    /*== change spaces to underscores in filename  ==*/

    $final_filename = str_replace(" ", "_", $imgfile_name);

    $newfile = $uploaddir . "/$final_filename";

   

    /*== do extra security check to prevent malicious abuse==*/

    if (is_uploaded_file($imgfile))

    {

 

      /*== move file to proper directory ==*/

      if (!copy($imgfile,"$newfile"))

      {

          /*== if an error occurs the file could not

              be written, read or possibly does not exist ==*/

          print "Error Uploading File.";

          exit();

      }

    }

unlink($imgfile);

 

   

    print("<img src=\"$final_filename\">");

 

$user = $_SESSION['MM_Username'];

 

if ($_POST['Submit']) {

if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {

//print_r($_FILES);

 

mysql_select_db($database_elvisdb, $elvisdb);

$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"), $_FILES['imgfile']['size']));

$query = sprintf("INSERT INTO images (usnm, ImageName, imageFile) VALUES ('$user','%s', '%s')", $photo, $_FILES['imgfile']['type']);

if (mysql_query($query)) {

$messages[] = "Your files is successfully store in database";

} else {

$messages[]= mysql_error();

}

} else {

$messages[] = "The file is bigger than the allowed size (96k) please reduce your file size";

}

 

 

}

?>

 

 

</head>

<body bgcolor="#FFFFFF">

 

    <h2>Upload and Resize an Image</h2>

 

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">

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

 

    <p>Upload Image: <input type="file" name="imgfile"><br>

    <font size="1">Click browse to upload a local file</font><br>

    <br>

    <input type="submit" value="Upload Image">

    </form>

 

</body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/41136-upload-image-problem/
Share on other sites

"Permission denied in C:\Program Files\xampp\htdocs\epeople\upim.php on line 81"

 

Is a permission problem. If it were a linux server, all you had to do is right-click the folder and set the chmod to, say, 777.

 

I don't know about windows though. Try googling "folder persmission for windows server" and things of the sort

Link to comment
https://forums.phpfreaks.com/topic/41136-upload-image-problem/#findComment-199232
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.