/    Sign up×
Community /Pin to ProfileBookmark

Delete uploaded images

Hello again, I will try and explain this as simply as possible.

I have some pages I have been working on

I have a form that inserts info into a databse and uploads an image to an upload directory all of this works fine.

I finished coding the function to delete the selected row.

I have done a massive search and found a few sites using “unlink() function”.
I dont quite understand how to use this function.

could one (or more) of you more advanced coders assist me in deleting the image linked to the row when the “delete” link is selected to delete the row.

here is the code for the upload form I use.

[code=php]
<?php
$uploadpath = ‘../uploads/’;
$source = $HTTP_POST_FILES[‘image’][‘tmp_name’];
$dest = ”;
// if page is not submitted to itself echo the form
if (!isset($_POST[‘scam’]))
{
?>
<form action=”<?php echo $PHP_SELF;?>” method=”post” enctype=”multipart/form-data” name=”scam” id=”scam”>
<center>

<font color=”#0000FF” size=”-1″>Upload a screen shot of the scam in progress</font>
<br>
<b>Screen Shot:</b> <font color=”#FF0000″>*</font>&nbsp;
<input name=”image” type=”file” id=”image” size=”32″>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”96000″>
<div align=”center”>
<input type=”submit” name=”scam” value=”Submit”>
<input name=”reset” type=”reset” class=”liteoption” value=”RESET” />
</div>
</form>
<?
} else {

if ( ($source != ‘none’) && ($source != ” )) {

$imagesize = getimagesize($source);

switch ( $imagesize[2] ) {

case 0:
die (“Image unknown, must be gif, jpg, or png ONLY!”);
break;

case 1:
$dest = $uploadpath.uniqid(‘img’).’.gif’;
break;

case 2:
$dest = $uploadpath.uniqid(‘img’).’.jpg’;
break;

case 3:
$dest = $uploadpath.uniqid(‘img’).’.png’;
break;

}

if ( $dest != ” ) {

if ( move_uploaded_file( $source, $dest ) ) {

chmod($dest, 0644);
} else {
die (“Unable to store image!”);
}

}

} else {
die (“No image was selected!”);
}

$image = $dest;
$query = “INSERT INTO “. scams .”
(scam_image) VALUES
(‘”.$image.”‘)”;
$result = mysql_query($query) or die (“Error retrieving data!”);
{
echo “scammer_created_succesfully!”;
}
}
?>
[/code]

Here is the code I use for outputing the query as well as delete the row:

[code=php]
<h1 align=”center”><b>Scammer Admin Area</b></h1>
<div align=’right’><A HREF=”<? echo $PHP_SELF ?>?action=logout”>Logout</A></div><br />
<table width=”98%” border=”1″ align=”center” cellpadding=”0″ cellspacing=”0″ bordercolor=”#c0c0c0″>
<br />
<br />
<tr>
<td width=”5%” nowrap=”NOWRAP”> <h3 align=”center”><b>Delete</b></h3></td>
</tr>
<tr>
<?php
$query = “SELECT * FROM scams ORDER by scam_id”;
$result = mysql_query($query) or die (“Error in query: $query. “.mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0)
{
// yes print them one after another
while($row = mysql_fetch_array($result)) {
$id = $row[‘scam_id’];

echo “<td width=’5%’ align=’center’><div align=’center’><a href=’$PHP_SELF?cmd=delete&amp;scam_id=$row[scam_id]’>Delete</a></div></td>”;
echo “</tr>”;
}
} else {
// no print status message
echo “<tr>”;
echo “<td COLSPAN=9 width=’100%’ align=’center’><b>No Scammers Have been listed at this time</b></td>”;
echo “</tr>”;
}
?>
</table>
<?php
if($_GET[“cmd”]==”delete”)
{
$scam_id = $HTTP_GET_VARS[‘scam_id’];

$query = “SELECT * FROM scams ORDER by scam_id;
$result = mysql_query($query) or die (“could not select!”);

$query = “DELETE FROM scams WHERE scam_id = $scam_id”;
$result = mysql_query($query) or die (“could not delete!”);
?>
[/code]

Thank you for your time

I do have a test area set up please pm me if you need the link

**had to remove much of my code due to limitations here (my scripts were over the 10000 limit ? )

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bradmasterxJan 12.2008 — This might do it i dont get where u want to unlink the file
[code=php]
<h1 align="center"><b>Scammer Admin Area</b></h1>
<div align='right'><A HREF="<? echo $PHP_SELF ?>?action=logout">Logout</A></div><br />
<table width="98%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#c0c0c0">
<br />
<br />
<tr>
<td width="5%" nowrap="NOWRAP"> <h3 align="center"><b>Delete</b></h3></td>
</tr>
<tr>
<?php
$query = "SELECT * FROM scams ORDER by scam_id";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0)
{
// yes print them one after another
while($row = mysql_fetch_array($result)) {
$id = $row['scam_id'];

echo "<td width='5%' align='center'><div align='center'><a href='$PHP_SELF?cmd=delete&amp;scam_id=$row[scam_id]'>Delete</a></div></td>";
echo "</tr>";
}
} else {
// no print status message
echo "<tr>";
echo "<td COLSPAN=9 width='100%' align='center'><b>No Scammers Have been listed at this time</b></td>";
echo "</tr>";
}
?>
</table>
<?php
if($_GET["cmd"]=="delete")
{
$scam_id = $HTTP_GET_VARS['scam_id'];

$query = "SELECT * FROM scams ORDER by scam_id";
$result = mysql_query($query) or die ("could not select!");

$query = "DELETE FROM scams WHERE scam_id = $scam_id";
$result = mysql_query($query) or die ("could not delete!");
$rowDel = mysqli_fetch_assoc($result) or die ("could not delete!");
unlink($rowDel['scam_image']);
?>
[/code]

Unlink is basicaly
[code=php]unlink("DIRECTORY/FILENAME.EXTENTION");[/code]
Copy linkTweet thisAlerts:
@caspert_ghostauthorJan 12.2008 — I dont understand your comment...

where to unlink the file?

When I upload the file (via a form from another page) it is given a 12 didget name, each upload (meaning each row) will have a unique id for the image.

when I click the delete link it deletes the row but I need to delete the image that was uploaded WITH that row.

i have tried many different ways to unlink but I keep getting errors:

I want to delete the file asociated with the database row of the ID I am deleting

the code you just suggested gave this error:

Fatal error: Call to undefined function mysqli_fetch_assoc() in C:wwwscammerscam_adminadmin_tables.php on line 137


I am running: PHP Version 5.2.5
Copy linkTweet thisAlerts:
@caspert_ghostauthorJan 12.2008 — I must be really tired..

your code works for me

I had to change from :

mysqli_fetch_assoc($result)

to

mysql_fetch_assoc($result)

thank you for your help my eyes are tired from searching,.
Copy linkTweet thisAlerts:
@bradmasterxJan 12.2008 — kk it ok i use improved mysql tht all.
×

Success!

Help @caspert_ghost spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...