SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Now available in Orange Tijmen's Avatar
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    1,469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Warning: unlink(c:/uploadimg/): Permission denied

    I have made the following script which allows people to upload a picture. When it runs for the first time everything works fine. But if you have uploaded a picture and decide to change it again, it's supposed to remove the data from the db(which is does) and to delete the image from the hd and replace it with the new one. But whenever it tries to delete the image from the hd after changing your picture it displays "Warning: unlink(c:/uploadimg/): Permission denied "

    The weird thing is that when your upload the image for the first time there is no acces denied, neither when you delete it. Only after you replace the image for the second time... Does anybody have a suggestion what i might do to solve this?


    PHP Code:
    if (isset($_POST['del_pic'])) {
                              
    $query "SELECT file_name FROM userpics WHERE user_id = {$_SESSION['user_id']}";
    $result mysql_query($query);
    $row mysql_fetch_object($result);

                              
    if (
    $row) {
                              
    unlink ("c:/uploadimg/$row->file_name");
    $query "DELETE FROM userpics WHERE user_id = {$_SESSION['user_id']}";
    $result mysql_query($query) or die (mysql_error());

                                
    if (
    $result) {
    echo 
    '<p><font color="CC3300">Your photo has been successfully deleted.</font></p>';

    } else  {
    echo 
    '<p><font color="CC3300">Your photo could not be removed due to a system error! Please contact the admin if the error persist!</font></p>';
    }
                              
    } else {
    echo 
    '<p><font color="CC3300">There is no photo to delete!</font></p>';
    }
    }
                            
    if (isset(
    $_POST['submit'])) {
                            
    if (
    $_FILES['upload']['type'] !== 'image/pjpeg') {
    echo 
    '<p><font color="CC3300">Invalid file type, only jpeg files are allowed!</font></p>';

    } else {

    //create random filename
    $rand md5(uniqid(rand(), true));
    $rand_short substr($rand, -10);
    $extension explode ('.' ,$_FILES['upload']['name']);
    $file_name $rand_short '.' $extension[1];
                
    //check if picname  exist
    $query "SELECT file_name FROM userpics WHERE user_id = {$_SESSION['user_id']}";
    $result mysql_query($query) or die(mysql_error());
    $num mysql_num_rows($result);
    $row mysql_fetch_array($resultMYSQL_NUM);
                            
    if (
    $num 0) {
                            
    //file exist
    if ($result) {
                                
    $query "UPDATE userpics SET file_name='$file_name' AND upload_date='NOW()' WHERE user_id = {$_SESSION['user_id']}";
    $result mysql_query($query) or die(mysql_error());
                                
    if (
    $row) {
    unlink ("c:/uploadimg/$row->file_name"); //del old file
    }

    }
    //file does not exist
    } else {
    $query "INSERT INTO userpics (user_id, file_name, upload_date) VALUES ('{$_SESSION['user_id']}', '$file_name', NOW())";
    $result mysql_query($query) or die(mysql_error());
    }
                                  
    // file successfully inserted!
    if ($result) {
    if (
    move_uploaded_file($_FILES['upload']['tmp_name'], "c:/uploadimg/$file_name")) {
                            
    echo 
    '<p><font color="CC3300">Your photo has been uploaded successfully!</font></p>';
                                     
    } else { 
    // file failed
                                     
    echo '<p><font color="CC3300">Your photo could not be processed due to a system error! Please contact the admin if the error persist!</font></p>';
                                     
    unlink ("c:/uploadimg/$row->file_name");
                                     
    $query "DELETE user_id, file_name, upload_date FROM userpics WHERE user_id = {$_SESSION['user_id']}";
                                     
    $result mysql_query($query) or die(mysql_error());
    // move file
    //result
    //file jpg
    //main 

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing this:
    Code:
    unlink ("c:/uploadimg/$row->file_name");
    to this:
    Code:
    @unlink ("c:/uploadimg/$row->file_name");
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    Now available in Orange Tijmen's Avatar
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    1,469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But the @ just removes the error from my screen, it still doesn't remove the file from the HD. Which it should do.

    One other thing that i forgot is that the first time the name appears in the db the way it should but the second time it just a zero...Which is a bit weird, because it's the same code that gets used twice.

  4. #4
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The warning error appears to me that the actual filename is not being passes through to the unlink(). If I am correct (which I may not be) that error should say "WARNING: unlink(c:/uploadimg/filename.jpg)..."

    Try echoing out the file name before the unlink() to be sure it's holding a good value.
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  5. #5
    Now available in Orange Tijmen's Avatar
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    1,469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I try'd it and it's empty.... But i don't really get why it should be empty.

  6. #6
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tijmen
    I try'd it and it's empty.... But i don't really get why it should be empty.
    What do you get when you do this:
    Code:
    $num_rows = mysql_num_rows($result);
    It's possible you have something wrong with your query and that's why you're not getting back a filename. In fact, you should probably have this for your if statement:
    Code:
    if ($num_rows > 0) {
    Saying if ($row) is incorrect. You're always going to have a result object, but it may not be what you're expecting.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  7. #7
    Now available in Orange Tijmen's Avatar
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    1,469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found out that the second time the file_name in the db is 0, but when you echo the query this shows up

    9b439ebc4a.jpg
    UPDATE userpics SET file_name='9b439ebc4a.jpg' AND upload_date='NOW()' WHERE user_id = 19

    So it does fill in the file name but instead of really doing that it makes it 0??

    I also tested the numrows they are 1 when he is trying to update, just as the result after the query, he does change the upload date but not the file_name?

  8. #8
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this instead:
    Code:
    $del_pic = $_POST['del_pic'];
    $userid =  $_SESSION['user_id'];
    
    if (isset($del_pic)) { 
                               
    $query = "SELECT file_name FROM userpics WHERE user_id = {$userid}"; 
    $result = mysql_query($query); 
    $row = mysql_fetch_object($result); 
    
                               
    if (mysql_num_rows($result) > 0) { 
                               
    unlink ("c:/uploadimg/$row->file_name"); 
    $query = "DELETE FROM userpics WHERE user_id = $userid"; 
    $result = mysql_query($query) or die (mysql_error()); 
    
                                 
    if ($result) { 
    echo '<p><font color="CC3300">Your photo has been successfully deleted.</font></p>'; 
    
    } else  { 
    echo '<p><font color="CC3300">Your photo could not be removed due to a system error! Please contact the admin if the error persist!</font></p>'; 
    } 
                               
    } else { 
    echo '<p><font color="CC3300">There is no photo to delete!</font></p>'; 
    } 
    } 
                             
    if (isset($_POST['submit'])) { 
                             
    if ($_FILES['upload']['type'] !== 'image/pjpeg') { 
    echo '<p><font color="CC3300">Invalid file type, only jpeg files are allowed!</font></p>'; 
    
    } else { 
    
    //create random filename 
    $rand = md5(uniqid(rand(), true)); 
    $rand_short = substr($rand, -10); 
    $extension = explode ('.' ,$_FILES['upload']['name']); 
    $file_name = $rand_short . '.' . $extension[1]; 
                 
    //check if picname  exist 
    $query = "SELECT file_name FROM userpics WHERE user_id = $userid"; 
    $result = mysql_query($query) or die(mysql_error()); 
    $num = mysql_num_rows($result); 
    $row = mysql_fetch_array($result, MYSQL_NUM); 
                             
    if ($num > 0) { 
                             
    //file exist 
    if ($result) { 
                                 
    $query = "UPDATE userpics SET file_name='$file_name' AND upload_date='NOW()' WHERE user_id = $userid"; 
    $result = mysql_query($query) or die(mysql_error()); 
                                 
    if ($row) { 
    unlink ("c:/uploadimg/$row->file_name"); //del old file 
    } 
    
    } 
    //file does not exist 
    } else { 
    $query = "INSERT INTO userpics (user_id, file_name, upload_date) VALUES ('$userid', '$file_name', NOW())"; 
    $result = mysql_query($query) or die(mysql_error()); 
    } 
                                   
    // file successfully inserted! 
    if ($result) { 
    if (move_uploaded_file($_FILES['upload']['tmp_name'], "c:/uploadimg/$file_name")) { 
                             
    echo '<p><font color="CC3300">Your photo has been uploaded successfully!</font></p>'; 
                                      
    } else { // file failed 
                                     echo '<p><font color="CC3300">Your photo could not be processed due to a system error! Please contact the admin if the error persist!</font></p>'; 
                                     unlink ("c:/uploadimg/$row->file_name"); 
                                     $query = "DELETE user_id, file_name, upload_date FROM userpics WHERE user_id = $userid"; 
                                     $result = mysql_query($query) or die(mysql_error()); 
    } // move file 
    } //result 
    } //file jpg 
    } //main
    Let us know if that helped.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  9. #9
    Now available in Orange Tijmen's Avatar
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    1,469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well the same error appears

    "Warning: unlink(c:/uploadimg/): Permission denied in c:\program files\apache group\apache\htdocs\html\travel\change_photo.php on line 273
    "
    This happens after you try you change your pictures for the second time. The value in the DB is correct the first time but 0 the second time. I don't have much time to try thing out right now, but i will to test it a little more tommorow when i have some more time!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •