Jump to content

Recommended Posts

Hey everyone,

 

I have been trying this code of mine for a while now, but I can't seem to get it to work. So basically, I have many areas of my site to upload images to, and one of these areas is the news.php page. (located in the base directory) To post a news item I first go to admin.php page (located in admin/admin.php) then click on postNews.php. Then I choose a title, etc.. and an image to upload all of which are sent to a MySQL database. The posting goes absolutely fine until I try and delete the post. When I try and delete the image (using the unlink function) I get an error telling me that no such directory can be found in the current one, which is understandable. (working from admin/deleteNews.php, images stored at: images/uploads/...). Here is some code...

 


  include("../scripts/db_connect_alt.php");

  $sql1 = "SELECT image FROM news WHERE id = '".$_POST['remove']."'";
  $query1 = mysql_query($sql1) or die ( mysql_error() );
  $x = mysql_fetch_assoc($query1);
  if (!($x['image'])) { } else { $r = "../" + $x['image']; unlink($r); }

 

$_POST['remove'] is the drop-down menu selection from deleteNews.php. $x['image'] looks like this: images/IMG_0986.jpg and so since I am in the wrong directory to access the uploads folder, I assumed adding a "../" on the front would do the trick.. but it didn't. The error I get is this: Warning: unlink(0) [function.unlink]: No such file or directory in I:\www\ula.co.nz\admin\brain.php on line 77 line 77 being the final line of the attached code.

 

Any help would be appreciated, and sorry, I am quite new to PHP and MySQL.

 

Thanks a bunch,

George.

Link to comment
https://forums.phpfreaks.com/topic/180748-solved-uploading-issue/
Share on other sites

This line is not quite right

if (!($x['image'])) { } else { $r = "../" + $x['image']; unlink($r); }

 

You should first use mysql_num_rows to check to see if the query returned any results first, eg

if(mysql_num_rows($query1) == 1)
{
    // query return a result
    // proceed to delete image
}

 

You'll find it'll be alot easier if you use full paths, rather then relative paths. You should set a variable which holds the full path to your uploads/ directory first.

$upload_base_dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';

 

Now that you have the path to your upload directory set you can easily append the variable $x['image'] to form the full path to the image.

$image_to_be_deleted = $upload_base_dir . $x['image'];

 

To deleter the image simply pass the $image_to_be_deleted to the unlink function.

 

if(mysql_num_rows($query1) == 1)
{
    // query return a result
    // proceed to delete image
    $upload_base_dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
    $image_to_be_deleted = $upload_base_dir . $x['image'];
    
    // check that file exists
    if(file_exists($image_to_be_deleted))
    {
        unlink($image_to_be_deleted);

        // delete image reference form database
        $query = "DELETE FROM news WHERE image = '".$x['image'];."'";
        mysql_query($query);

        echo 'Image deleted';
    }
    else
        echo 'Cannot delete the image as it cannot be found!';
}
else
    echo 'Query returned no results';

Hey, well I tried this out (thank you very much btw!) but it just didn't work. The image isn't deleted from the folder it is in. This is the code (attached above) modified to fit my page. I'll just post the entire section of code.

 

... other code ...<?php
else if ($type == 'deleteNews') {

if (!($_POST['remove'])) {
header("Location: http://".$_SERVER['HTTP_HOST']. dirname($_SERVER['PHP_SELF'])."/deleteNews.php?error=1");
}
else {
	include("../scripts/db_connect_alt.php");

	$sql1 = "SELECT image FROM news WHERE id = '".$_POST['remove']."'";
  		$query1 = mysql_query($sql1) or die ( mysql_error() );
		$x = mysql_fetch_assoc($query1);

	if(mysql_num_rows($query1) == 1)
		{
		// query return a result
		// proceed to delete image
		$upload_base_dir = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/';
		$image_to_be_deleted = $upload_base_dir.$x['image'];

		// check that file exists
		if(file_exists($image_to_be_deleted))
			{
			unlink($image_to_be_deleted);
			} 

	$sql = "DELETE FROM news WHERE id = '".$_POST['remove']."'";
	$query = mysql_query($sql) or die ( mysql_error() );
	if ($query == 1) {
		$success = 1;
		}
	}
}
} ?>

 

On the previous page is this:

 

<form action="brain.php?type=deleteNews" method="post" enctype="multipart/form-data" name="deleteNews">
  ...
        <select name="remove" id="remove">
          <option value="null">Select a post to remove:</option>
          
          <?php while ($results = mysql_fetch_assoc($query)) { //results from the search query selecting all news post titles
		echo '<option value="'.$results['id'].'">'.$results['title'].'</option>
          '; }
	  ?>
        </select>

 

So I don't quite know what to do in terms of how to approach the problem. The rest of the news posts are deleted no matter what, but whenever I check my uploads folder, I find that the image has not been unlinked..

 

Thanks again,

George.

Ok, so I mucked around with my original idea for a while, and I solved this problem using a relative link rather than the suggested actual link. Final code is:

 

<?php
$sql1 = "SELECT image FROM news WHERE id = '".$_POST['remove']."'";
  		$query1 = mysql_query($sql1) or die ( mysql_error() );
		$x = mysql_fetch_assoc($query1);

	if(mysql_num_rows($query1) == 1)
		{
		// query return a result
		// proceed to delete image
		$upload_base_dir = "../";
		$image_to_be_deleted = $upload_base_dir.$x['image'];
		$_SESSION['print'] = $image_to_be_deleted;

		// check that file exists
		if(file_exists($image_to_be_deleted))
			{
			unlink($image_to_be_deleted);
			} 

	$sql = "DELETE FROM news WHERE id = '".$_POST['remove']."'";
	$query = mysql_query($sql) or die ( mysql_error() );
?>

 

Cheers for the help organising the guts of this solution.

 

Regards,

George.

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.