PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 27th, 2005, 01:51 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
PHP problem - bit complicated. Can anyone have a look? Thanks

Hi people. I'm new. Im working on a php content management system as part of a project and im having some trouble with my admin script.

What the following script does is bring together all unauthorised user submitted files. When files are submitted via a simple web form (different script) the script makes use of the $_FILES multidimensional array, inserting the properties of the file to the 'uploads' table, and another function stores the file into its physical location on the server.

Along with these descriptive attributes (upload_id, size, type ect) is an 'approved' field which is automatically set to 'N' (i.e. non approved).

Here is my 'uploads' db scheme:

CREATE TABLE uploads(
upload_id int( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
file_name VARCHAR( 30 ) NOT NULL ,
file_size INT( 6 ) UNSIGNED NOT NULL ,
file_type VARCHAR( 30 ) NOT NULL ,
description VARCHAR( 100 ) DEFAULT NULL ,
upload_date DATETIME NOT NULL ,
approved CHAR(1) DEFAULT 'N',
PRIMARY KEY ( upload_id ) ,
KEY file_name( file_name )
)

My view_files script checks checks if the file exists on the server, and then its approved status in the upload table. It will only display files with a 'approved' set to 'Y'. If they are approved, the files are displayed with a link for download - I hope i havent lost anyone here.

Ok so my admin script collects together all files that have their approved field set to 'N' - non approved. It lists the file name, type and a link for previewing the file.

On this script I have checkboxes next to the files, along with an approved and delete button. Selecting which files via the checkboxes and hitting the 'approve' button runs an SQL update query to change the selected files' approved status to 'Y' - thereby making them viewable on the users' side and removing them from the list of 'currently unauthorised' files.

The checkbox is part of an array that is constructed using a foreach() function..i.e. '"if the approve button is clicked, for each checkbox selected, do the following"

NOW then..I also have the delete button, the purpose of which is to remove any dodgy files that may have been submitted. This makes use of the same checkbox array. What I want this to do is for each checkbox selected, not only delete the upload_id from the database, but also the actual file on the server.

Here is the code:

<?php #Admin submission approval check

$page_title = "View Files";
include_once ('../includes/cms_admin_header.html');

require_once ('../../../mysql_connect2.php');//opens the database connection

if (isset($_POST['submit'])) {

foreach ($_POST['select'] as $key => $value) { // For each item selected do the following.

// $key is the file's unique id
$query = "UPDATE uploads SET approved = 'Y' WHERE upload_id = '$key'";
$result = mysql_query($query);
}
}
if ($result) { // If it ran OK, display the message
echo '<h3>Files successfully added to the database</h3>';
include_once ('../includes/cms_admin_footer.html');
exit();
}

if (isset($_POST['delete'])) {//if delete is pressed

foreach ($_POST['select'] as $key => $value) {//for each item selected do the following

// $key is the file's unique id
$query = "DELETE from uploads WHERE upload_id = '$key'";
$result = mysql_query($query);
}
}

if ($result) { // If it ran OK, display the message
echo '<h3>Files successfully deleted</h3>';
include_once ('../includes/cms_admin_footer.html');
exit();
}

echo "Greetings Administrator";

$query = "SELECT upload_id, file_name, file_type, description, DATE_FORMAT(upload_date, '%M %e, %Y') AS d FROM uploads WHERE approved = 'N' ORDER BY upload_date DESC";
$result = mysql_query($query);

if ($result) {

echo "<p><b>All the user submitted files</b></p>";

//Begin Parsing of data into table
echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\" />
<table border=\"1\" cellpadding=\"4\" width=\"100%\">
<tr>
<td>
<b>Name</b>
</td>
<td>
<b>Type</b>
</td>
<td>
<b>Description</b>
</td>
<td>
<b>Link</b>
</td>
<td>
<b>Date Submitted</b>
</td>
<td>
<b>Select</b>
</td>
</tr>";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

echo " <tr>
<td>
{$row['file_name']}
</td>
<td>
{$row['file_type']}
</td>
<td>
{$row['description']}
</td>
<td>
<a href=\"../download_file.php?uid={$row['upload_id']}\">Preview</a>
</td>
<td>
{$row['d']}
</td>
<td>
<input type=\"checkbox\" name=\"select[{$row['upload_id']}]\" value=\"1\">
</td>";
}

echo " </tr>
</table>";

echo "
<INPUT TYPE=\"submit\" name=\"submit\" value=\"Approve\">
<INPUT TYPE=\"submit\" name=\"delete\" value=\"Delete\">";

} else {

echo '<p>There has been an error</p><p>' . mysql_error() . '</p>';

}

include_once ('../includes/cms_admin_footer.html');
?>


Right now, when i hit the delete button, nothin happens. All i want it to do is delete the selected upload_ids from the table, thereby removing its trace in my database. I would also like to delete the actual file from the server but im not sure how to do that.

When i hit the approve button, my update query works fine and the approved status is changed and the files no longer appear on my admin script.

Basically my DELETE query just refuses to work. If I change that query with any other type of query it works when i hit the delete button..but the DELETE FROM query itself just falls flat. To test this i assigned the exact same code as the approved button to the delete button..so now selecting files and hitting either button (approve/delete) does exactly the same thing.

I a completely stumped. I have no idea what to do

Just wondered if someone could have a look for me..i am new to php and maybe im not seeing an obvious error. I just want to be able to delete selected file records (via checkboxes) from my uploads table.

Thanks for your time.

Kind regards

-Pallaris

Reply With Quote
  #2  
Old February 27th, 2005, 03:39 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
The first thing I'd do is add an 'or die(mysql_error())' to the end of your mysql_query() call to see what the database'll tell you. The code looks ok, though, so my first guess is that the user you're connecting as doesn't have DELETE privileges.

Reply With Quote
  #3  
Old February 27th, 2005, 04:09 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Madpawn, thank you so much. Now I feel so stupid..the problem was, as you suspected, the lack of DELETE privileges for the user i was connecting as!!

Thanks for pointing out the obvious. Honestly..I should have thought of that one.

So that now successfully removes the file description from the uploads table. What I would like to do is also have the ability to remove the actual file from the folder on the server (where all the uploads are stored). I'm not so sure how to do this..something about the unlink() function. Could this be part of the delete query in my code...i.e..for all items selected, remove the record from the database..AND the file from location 'x'.

Any suggestions on how to do this would be most weclome. Thanks a lot for your help.

Regards

-P

Reply With Quote
  #4  
Old February 27th, 2005, 04:47 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
You just need to have the filename and unlink() it. It would probably be easiest to just SELECT the filename before you remove the record, but there are other options if you don't want to run a separate SELECT each time.

The best option would probably be to actually set the filename to it's id as it's uploaded, then you can use the id passed through your form to do both.

Actually, something like this may work, since you've already got all your upload_id's in an array:

PHP Code:
if (isset($_POST['delete'])) 
  {
     
//get filenames to delete
     
$query "SELECT
                     file_name,
                     upload_id
                    FROM
                      table
                    WHERE
                     upload_id
                       IN
                      (
$_POST['select'])";
    
$result mysql_query($query) or die(mysql_error());
  
    
//remove files from db and server
    
while( $row mysql_fetch_assoc($result) )
    {
       
//delete from db
       
$del_query "DELETE from TABLE WHERE upload_id = $row['upload_id']";
       
mysql_query($del_query);
  
      
//remove file
      
unlink('path/to/upload/dir/' $row['file_name']);
    }
  } 


I know it seems redundant to reselect your upload_id, but that way, you can remove the file from the db and the filesystem at the same time without the headache of aligning your select array and your form array.

Reply With Quote
  #5  
Old February 28th, 2005, 07:39 AM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Hey Mudpwan, thanks for the tip. I've tried using that code (changed a few parameters). here it is:

PHP Code:
if (isset($_POST['delete'])) 
{
     
//get filenames to delete
     
$query "SELECT file_name upliad_id, FROM uploads WHERE upload_id IN ($_POST['select'])";    
     
$result mysql_query($query) or die(mysql_error());
 
    
//remove files from db and server
    
while( $row mysql_fetch_assoc($result) )
    {
     
//delete from db
     
$del_query "DELETE from TABLE WHERE upload_id = $row['upload_id']";
     
mysql_query($del_query);
 
     
//remove file
     
unlink(../../../uploads' . $row['file_name']);//files are named by their upload_id, I tried entering this here but still didnt work.
    }

if ($result) { echo '
<h3>Files successfully deleted</h3>';
include_once ('
../includes/cms_admin_footer.html');
exit();



But i am getting the following error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\web\project\cms\admin\check_submissions.php on line 26 - which happens to be the first query line.

Im not sure what the problem is - the query looks fine to me

So after some playing around i tried this:

PHP Code:
if (isset($_POST['delete'])) {//if delete is pressed
 
foreach ($_POST['select'] as $key => $value) {
 
$query "DELETE FROM uploads WHERE upload_id = $key";
$result mysql_query($query);
//remove file
unlink('../../../uploads' $row['upload_id']);
}

if (
$result) { echo '<h3>Files successfully deleted</h3>';
include_once (
'../includes/cms_admin_footer.html');
exit();
}
 
 
if (
$result) { // If it ran OK, display the message
echo '<h3>Files successfully deleted</h3>';
include_once (
'../includes/cms_admin_footer.html');
exit(); 



Which is pretty much what i had before, except with the inclusion of the unlink() as partof the clause. This sucessfully deletes selected upload records from the uploads table but the unlink doesnt seem to work and i'm greeted with this error:

Warning: unlink(../../../uploads): Permission denied in c:\web\project\cms\admin\check_submissions.php on line 52


I tried this online on my server however and i get a different message:

PHP Warning: unlink(../../../uploads/): Is a directory in ............../project/cms/admin/check_submissions.php on line 52


Is there anything obviously wrong here? Any advice would be much appreciated. Thanks a lot for pointing me in the right direction here....i'm very grateful.

Regards

-P

Reply With Quote
  #6  
Old February 28th, 2005, 11:30 AM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
Yeah, the first thing's my fault -- for it to work, you need to convert $_POST['select'] to a comma-separated string first.

Either way, though, you'll still have the filepath errors. Without having your setup in front of me, I won't be able to help much with that. I'm pretty terrible with filepath issues myself, and I usually just end up having to pound at it until it works. Your local system's problem sounds like it's just a permissions issue, though -- check your uploads folder to see if the user you're logged in as has remove permissions.

Reply With Quote
  #7  
Old February 28th, 2005, 12:28 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Hey Mudpawn..

Quote:
you need to convert POST['select'] to a comma-separated string first
.

Im not sure i fully understand that. Do you mean define what POST['select'] is first?

Also just to be sure i've set global priveleges to the mysql user im using to connect and im still getting he permission denied error.

Online though the error is different again - warning me that ../../../uploads/ is in fact a directory.

edit - ok i've managed to get your script to work with this:

PHP Code:
if (isset($_POST['delete'])) 
{
     
//get filenames to delete
     
$query "SELECT file_name upliad_id, FROM uploads WHERE upload_id IN ('$_POST[select]')";    
     
$result mysql_query($query) or die(mysql_error());
    
//remove files from db and server
    
while( $row mysql_fetch_assoc($result) )
    {
     
//delete from db
     
$del_query "DELETE from TABLE WHERE upload_id = ('$row[upload_id]')";
     
mysql_query($del_query);
     
//remove file
     
unlink('../../../uploads/' $row['file_name']);//files are named by their upload_id, I tried entering this here but still didnt work.
    
}

if (
$result) { echo '<h3>Files successfully deleted</h3>';
include_once (
'../includes/cms_admin_footer.html');
exit();



except now i get this error after clicking the delete button:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM uploads WHERE upload_id IN ('Array')' at line 1

:\..

Reply With Quote
  #8  
Old February 28th, 2005, 12:42 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
$_POST['select'] is an array, and you can't just dump arrays into strings the way I did -- I was typing faster than I was thinking, unfortunately. You could use implode() to change that into a string:

PHP Code:
//say $_POST['select'] contains the ids
 //1, 3, and 5
 
$select_list implode(','$_POST['select']);
 
 echo 
$select_list//echos 1,3,5 


Then you can dump $select_list into the query. Sorry 'bout that.

In the code you posted, your unlink is looking for upload_id, not file_name. If the filename's different, you will have to pull it out of the db before you can try to unlink() it, of course. I think the error you got from your server, though, came from the fact that you don't have a slash between your uploads directory name and the filename, so your path is being read as ../../../uploadsfilename instead of ../../../uploads/filename.

As for your local error, your MySQL permissions aren't the ones unlink() checks. You need to check the uploads folder itself -- it will also have a set of permissions that determine who can delete objects inside that folder.

Reply With Quote
  #9  
Old February 28th, 2005, 01:01 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Im using windows XP, when i right click the uploads folder within my webserver I only have the following options under the sharing tab:

- share folder on network - checked
-allow network users to change files - checked

and under the general tab I can hit advance i can uncheck the 'read only' box..and apply.

I can't see anywhere else where i can change permissions.. any ideas?

Reply With Quote
  #10  
Old February 28th, 2005, 01:53 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Ok, I now have the following code which produces no error:

PHP Code:
if (isset($_POST['delete'])) {
  
     foreach (
$_POST['select'] as $key => $value) {
       
    
//delete from db
       
$query "DELETE FROM uploads WHERE upload_id = '$key'";
       
$result mysql_query($query);
  
    while( 
$row mysql_query($result) )
      
//remove file
       
unlink('../../../uploads/' $row['file_name']);
  
        if (
$result) { echo '<h3>Files successfully deleted</h3>';
  include_once (
'../includes/cms_admin_footer.html');                      
  exit();           
    }
  }



Trouble is the files aren't removed from the server, but the upload record is successfully deleted from the uploads table.

So basically i think i've narrowed it down to the unlink line..

Reply With Quote
  #11  
Old February 28th, 2005, 02:20 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
The problem is that you're trying to loop through the results of your DELETE query (with another mysql_query() call, too). DELETE queries only return the number of rows deleted. Try it like this instead:

PHP Code:
if (isset($_POST['delete'])) {
   
      foreach (
$_POST['select'] as $key => $value) {
        
     
//delete from db
        
$query "DELETE FROM uploads WHERE upload_id = '$key'";
        
$result mysql_query($query);
   
 
 
   
         if (
$result) { 
         
unlink('../../../uploads/' $row['file_name']);
  
  echo 
'<h3>Files successfully deleted</h3>';
 include_once (
'../includes/cms_admin_footer.html');                     
   exit();           
     }
   }
 } 


Of course, you're still not going to have a $row['file_name'] unless you run a SELECT somewhere.

Reply With Quote
  #12  
Old February 28th, 2005, 02:39 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
OK, thanks, so this is what I now have:

PHP Code:
if (isset($_POST['delete'])) {
   
     foreach (
$_POST['select'] as $key => $value) {
       
    
//delete from db
    
$query "SELECT file_name FROM uploads WHERE upload_id = '$key'"
       
$query "DELETE FROM uploads WHERE upload_id = '$key'";
       
$result mysql_query($query);
   
       if (
$result) { 
       
unlink('../../../uploads/' $row['file_name']);
  
    echo 
'<h3>Files successfully deleted</h3>';
  include_once (
'../includes/cms_admin_footer.html');                     
     exit();           
    }
   }




But im still getting the permissions error. And now i get the exact same error online on my host.

arrgh man this is killin me..:\

$row['file_name']); - i think this bit here(variable) is the problem. Basically i cant see where its defined anywhere in my clause.

Is that SELECT query ok?

Reply With Quote
  #13  
Old February 28th, 2005, 03:12 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
Both of your queries are called $query, so you're just overwriting your SELECT with your DELETE. The two have to be separate -- separate variables and separate mysql_query() calls.

Once you have your SELECT results dumped into a variable, you'll have to pull it out with one of the mysql_fetch functions, too.

Reply With Quote
  #14  
Old February 28th, 2005, 03:59 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Thanks..im trying your suggestions but i think i've made a hash of it:

PHP Code:
if (isset($_POST['delete'])) {
    
//for each item selected
     
foreach ($_POST['select'] as $key => $value) {
       
    
//select file name from uploads table
    
$query "SELECT file_name FROM uploads WHERE upload_id = '$key'"
    
$result mysql_query(query);
 
//delete from db  
       
$query1 "DELETE FROM uploads WHERE upload_id = '$key'";
       
$result1 mysql_query($query);
 
//delete from server
       
while ($row mysql_fetch_assoc($result));  
    
    
//if ($result) {  
    
unlink('../../../uploads/' $row['file_name']);
  
      echo 
'<h3>Files successfully deleted</h3>';
    include_once (
'../includes/cms_admin_footer.html');                     
       exit();           
    } 
 } 


Could you please look at that? Its saying:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

what am i doing wrong? * Im sorry about this..i'm new to php and server side code in generall..i guess i've taken on more than i should have with this project. Thankfully this is the last hurdle.

Reply With Quote
  #15  
Old February 28th, 2005, 04:08 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
You left out the dollar sign in front of query in your msyql_call(). Otherwise, it looks like it should work, barring filesystem unlink() issues.

Reply With Quote
  #16  
Old February 28th, 2005, 07:25 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
OK man, here is my revised code:

PHP Code:
if (isset($_POST['delete'])) {
foreach (
$_POST['select'] as $key => $value) {
//delete from db
$query "SELECT file_name FROM uploads WHERE upload_id = '$key'";
$result mysql_query($query);
$row mysql_fetch_array($resultMYSQL_ASSOC);
mysql_free_result($result);
$query "DELETE FROM uploads WHERE upload_id = '$key'";
$result mysql_query($query);
if (
$result) {
unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/' $row['file_name']);
             
echo 
'<h3>Files successfully deleted</h3>';
include_once (
'../includes/cms_admin_footer.html');
exit();
}
}} 


So i followed your suggestions, changed a few things such as absolute paths for the unlink part...and with some help i've managed to get the unlink function to actually look for the files i've selected.

As a test i cleared my uplaods folder and upload maths.doc. I went to my admin script, selected this file and hit delete. The upload record is removed alright but the unlink error now reads

Warning: unlink(c:/web/uploads/Maths.doc): No such file or directory in c:\web\project\cms\admin\check_submissions.php on line 38

And thats because the files are named by their upload_id on the server so Maths.doc doesnt exist, its actually 67.doc ect..

I tried substituting file_name for upload_id...back to permissions error. Then i tried selecting the file_type in the initial select query and have it so the unlink looks for 'upload_id, file_type'..so you have a number followed by the doc type..if that makes sense..but it kept producing errors.

Basically i want the unlink to look in the uploads folder and delete the file, which is simply the selected upload_id followed by its doc type.

How can i express this?

Thanks for your time.

Regards
-P

Reply With Quote
  #17  
Old March 1st, 2005, 11:30 AM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Ok, another revision of this unlink section.

PHP Code:
if (isset($_POST['delete'])) {
foreach (
$_POST['select'] as $key => $value) {
     
  
//get filenames to delete
     
$query "SELECT upload_id, file_name =('$fn') FROM uploads WHERE upload_id = $key";    
     
$extension explode ('.'$fn);
  
$result mysql_query($query) or die(mysql_error());
   
    
//remove files from db and server
    
while( $row mysql_fetch_assoc($result) )
    {
     
//delete from db
     
$del_query "DELETE from uploads WHERE upload_id = $key";
     
mysql_query($del_query);
     
//remove file
    
unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/' $row['upload_id'] . '.' $extension);      }
  } 
}
if (
$result) { echo '<h3>Files successfully deleted</h3>';
include_once (
'../includes/cms_admin_footer.html');
exit();




What im trying to do is call in the file_name, and seperate its extension from its name using the explode function and storing the extension in the variable $extension.
Im then trying to unlink the file, pointing to the server root, and im trying to express upload_id/$extension, but im getting the following error:

Warning: unlink(c:/web/uploads/120.): No such file or directory in blah blah

but this is ok (c:/web/uploads/120.):..is identifying the right id number,..just the extension of the file is missing..

is there anything obviously wrong?

Reply With Quote
  #18  
Old March 1st, 2005, 12:04 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
Where's $fn coming from?

Reply With Quote
  #19  
Old March 1st, 2005, 01:25 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Well I thought i'd set file_name to a variable, to store the new file name after its extension had been seperated.

What do you suggest?

Reply With Quote
  #20  
Old March 1st, 2005, 03:47 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 15
What I mean is, where is $fn getting its value? From the code I've seen, it looks like you're trying to explode() an unset variable, which will just give you a boolean FALSE. If $fn is set correctly, explode should be fine.

Also, explode() returns an array of the different pieces of your string, so if you just try to add $extension like you're doing, you'll just get filename.Array...

Another option would be to store the extension in the db and get it that way.

Reply With Quote
  #21  
Old March 1st, 2005, 06:07 PM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
So basically, can i get it to work so that file_name is no longer i.e 'word.doc', but just the '.doc'?, so that this new version of filename (essentially the extension) can be combined with the upload_id to form the actual file name as it exists on the server?

If so, how can i express this within my code above? Thanks in advance for any pointers.

Regards

-P

Reply With Quote
  #22  
Old March 2nd, 2005, 08:56 AM
Pallaris Pallaris is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 13 Pallaris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 18 sec
Reputation Power: 0
Cracked it by seperating the extension and setting this to the $extension variable

PHP Code:
if (isset($_POST['delete'])) {
foreach (
$_POST['select'] as $key => $value) {
     
  
//get filenames to delete
     
$query "SELECT upload_id, file_name FROM uploads WHERE upload_id = $key";    
  
  
$result mysql_query($query) or die(mysql_error());
    
//remove files from db and server
    
while( $row mysql_fetch_assoc($result) )
    {
     
//delete from db
     
$del_query "DELETE from uploads WHERE upload_id = $key";
     
mysql_query($del_query);
     
//remove file
 
$extension substr($row['file_name'] , (strrpos($row['file_name'] , '.') ? strrpos($row['file_name'] , '.')+strlen($row['file_name'] )), strlen($row['file_name'] ));
    
unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/' $row['upload_id'] . '.' $extension);    //files are named by their upload_id, I tried entering this here but still didnt work.
    
}
  } 
}
if (
$result) { echo '<h3>Files successfully deleted</h3>';
include_once (
'../includes/cms_admin_footer.html');
exit();



Mudpawn, thanks a lot for all yoru advice and guidance. I'm very grateful.

Kind regards

-P

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > PHP problem - bit complicated. Can anyone have a look? Thanks


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap