Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete files

Status
Not open for further replies.

brownfox

Programmer
Jan 5, 2003
173
GB
I have a script that allows a user to upload an image to a specified dir on my site and holds a record of the file path/name in my DB. Once the image is uploaded, my page displays the image with a option to delete the image (if for example it got corrupted on upload etc.). I have got it so thet the DB is updated if the user clicks delete but I'm having a hard time deleting the actual image file. unlink() doesn't work because I havn't stored the image as a temporary file first (I think). I have the right permissions set on my folders - so I don't think it's that either. Does anyone know how to delete a file using PHP?
 
Here are the functions that my company uses to upload and validate ppictures, they problably have gotten them in an web page. They usually don't delete the files, they keep them there and can upload other/the same file over that one, replacing it...


function validate_upload($the_file, $my_max_file_size, $image_min_width, $image_min_height,
$image_max_width, $image_max_height, $allowed_types, $the_file_type,
$registered_types) {
if ($the_file == '') { # do we even have a file?
$error .= "Pick a foto.";
} else { # check if we are allowed to upload this file_type
if (!in_array($the_file_type,$allowed_types)) {
$error .= "Error. The image must be in the following file types:";
while ($type = current($allowed_types)) {
$error .= &quot;\n<li>&quot; . $registered_types[$type] . &quot; (&quot; . $type . &quot;)</li>&quot;;
next($allowed_types);
}
}
if (ereg(&quot;image&quot;,$the_file_type) && (in_array($the_file_type,$allowed_types))) {
$size = GetImageSize($the_file);
list($foo,$width,$bar,$height) = explode(&quot;\&quot;&quot;,$size[3]);
if ($width > $image_max_width) {
$error .= &quot;Error: The imge size can't be higher than &quot; . $image_max_width . &quot; pixels width.&quot; . &quot;<br>&quot;;
}
if ($height > $image_max_height) {
$error .= &quot;Error: The image can't be more than &quot; . $image_max_height . &quot; Pixels de height&quot;;
}
if ($image_min_height > 0) {
if ($height < $image_min_height ) {
$error .= &quot;Error: The image can't be less than &quot; . $image_max_height . &quot; pixels height&quot;;
}
}
if ($image_min_width > 0) {
if ($width < $image_min_width ) {
$error .= &quot;Error: The image can't be less than &quot; . $image_max_width . &quot; Pixels width&quot;;
}
}
}
if ($error) {
return $error;
} else {
return false;
}
}
} # END validate_upload
# --



function upload($the_file, $the_path, $the_file_name, $my_max_file_size, $image_min_width,
$image_min_height, $image_max_width, $image_max_height, $allowed_types,
$the_file_type, $registered_types) {

$error = validate_upload($the_file, $my_max_file_size, $image_min_width, $image_min_height,
$image_max_width, $image_max_height, $allowed_types, $the_file_type,
$registered_types);

if ($error) {
return $error;
} else { # cool, we can continue
if (!@copy($the_file, $the_path . &quot;/&quot; . $the_file_name)) {
$tam = $my_max_file_size/1024;
return &quot;Couldn't refresh the image. max $tam Kb.&quot;;
}
}
} # END upload


Hope it may be usefull...
 
with this
<?
$stringURL=&quot;$everyValues=explode(&quot;/&quot;,$stringURL); //separate the values within &quot;/&quot;
$countArray=count($everyValues); //count how many elements are in the array
echo $everyValues[$countArray]; //print only the last one, that should be my_pic.jpg
?>
 
Or...... use the built in PHP function pathinfo

Code:
<?php
$stringURL = &quot;[URL unfurl="true"]http://mysite.com/images/my_pic.jpg&quot;;[/URL]
$path_parts = pathinfo($stringURL);

echo $path_parts[&quot;basename&quot;];

?>

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top