Jump to content

File unlink from another directory


cobusbo

Recommended Posts

Hi im running a script to remove files from a directory but seems like I'm doing something wrong with the paths to the file

 

First let me start with the field in my database.

 

its stored as

 

images/filename.jpg

 

The fulle path to the file is

 

chat2/profile/images/filename.jpg

 

The file with the script im trying to run is in the chat2 folder 

 

I got the following stored in my script

$pprofilepic = "../profile/".$rowbd['pprofilepic'];
unlink($pprofilepic);

but im getting the error

 

 

 

Warning: unlink(../profile/images/resized_1198_1448338715.jpg): No such file or directory in /home/zhetnsdd/public_html/chat2/cron.php on line 16

 

Link to comment
Share on other sites

it's a single . [dot]

./ = start in current directory

../ = start in parent directory

Not helping

 

<?php
   include("chat_code_header.php");


$querybd = "SELECT Username, last_online, pprofilepic FROM Users2
WHERE last_online < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))"; 


$resultbd = mysql_query($querybd) or die(mysql_error());


$users = array();
while($rowbd = mysql_fetch_array($resultbd)){
if ($rowbd['Username'] != '')
$users[] = $rowbd['Username'];
if($rowbd['pprofilepic'] == ""){}else{
$pprofilepic = "./profile/".$rowbd['pprofilepic'];
print "\n Pictures Removed:\n" . $pprofilepic . "\n";
unlink($pprofilepic);
}
}
    $list = implode('; ', $users);
if($list == ""){printf ("Nobody Removed\n");}else{
print "\n Users Removed:\n" . $list . "\n";


}
printf("Users Deleted: %d\n", mysql_affected_rows());
   $sqldel = "DELETE Users2, StringyChat, pm, namechanges, kicksamount, broadcast, timeban
FROM Users2
    LEFT JOIN StringyChat ON Users2.mxitid = StringyChat.StringyChat_ip
    LEFT JOIN pm ON Users2.mxitid = pm.mxitid
    LEFT JOIN namechanges ON Users2.mxitid = namechanges.userid
    LEFT JOIN kicksamount ON Users2.mxitid = kicksamount.mxitid
    LEFT JOIN broadcast ON Users2.mxitid = broadcast.mxitid
    LEFT JOIN timeban ON Users2.mxitid = timeban.mxitid
WHERE Users2.last_online < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))";


mysql_query($sqldel) or die("Error: ".mysql_error());




$query = "DELETE FROM StringyChat
WHERE StringyChat_time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY))"; 
    mysql_query($query);
printf("Messages deleted: %d\n", mysql_affected_rows());
$query1 = "DELETE FROM pm
WHERE time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY))"; 
    mysql_query($query1);
printf("Private Messages deleted: %d\n", mysql_affected_rows());




   
?>

 and output is as follow

 

 

Pictures Removed:

./profile/images/resized_976_1447730821.jpg
 
Warning: unlink(./profile/images/resized_976_1447730821.jpg): No such file or directory in /home/zhetnsdd/public_html/chat2/cron.php on line 16
 
 Pictures Removed:
./profile/images/resized_1380_1448867798.jpg
 
Warning: unlink(./profile/images/resized_1380_1448867798.jpg): No such file or directory in /home/zhetnsdd/public_html/chat2/cron.php on line 16
 
 Users Removed:
Ernest; The man; Mrs pelton; Quinton; Refresh; Don carter; Kevin; Orange; Edison; Max; Cagy LPD; Salie; knoxman; dcnator ; Melany; NOMFUNDO; Princess; me; Gummy bear; Back; Leilo; michael; Man x; Hot chick; Jason; Mpendulo
Users Deleted: 26
Messages deleted: 290
Private Messages deleted: 140

 

Link to comment
Share on other sites

do these images work correctly when your chat code displays them by retrieving the 'pprofilepic' value and producing an <img link?
 
are you sure that the images exist and haven't already been removed?
 
relative paths are relative to the current working directory. what does adding the the following to the script show - 

echo getcwd();
echo '<br>';
echo __FILE__;

 
 also, please browse to one of these images and post the URL for us to see. if you don't want to post your domain name, xxxx it out, but don't change anything that's after the domain name.
 
you could always form an absolute path, starting with '/home/zhetnsdd/public_html/chat2/profile/' and concatenate the 'pprofilepic' value it. if you are going to run this script via a cron job (you are apparently browsing to it now), i'm not sure what the correct env variable would be to build that dynamically.

Link to comment
Share on other sites

do these images work correctly when your chat code displays them by retrieving the 'pprofilepic' value and producing an <img link?

 

are you sure that the images exist and haven't already been removed?

 

relative paths are relative to the current working directory. what does adding the the following to the script show - 

echo getcwd();
echo '<br>';
echo __FILE__;

 

 also, please browse to one of these images and post the URL for us to see. if you don't want to post your domain name, xxxx it out, but don't change anything that's after the domain name.

 

you could always form an absolute path, starting with '/home/zhetnsdd/public_html/chat2/profile/' and concatenate the 'pprofilepic' value it. if you are going to run this script via a cron job (you are apparently browsing to it now), i'm not sure what the correct env variable would be to build that dynamically.

Yes I'm trying to run the script as a cron job every 24h

http://xxxxxxxx/chat2/profile/images/resized_1380_1448867798.jpg

Do I need to include the full path to unlink the file because in the previous I had a file in the profile folder which I just included the 

$rowbd['pprofilepic']

I will only be able to test the rest of the codes you gave me once there is a user to remove with an image

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.