Jump to content

Delete image from folder?


Savi

Recommended Posts

Hi

I found this great script that works just the way I want it, simple and usefull.
HOWEVER the delete function doesn't work. I can't seem to figure out a reason, maybe you can see why?

It is the function on line 191 I'm talking about.
[code]<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<meta http-equiv="content-language" content="en-us">
<meta http-equiv="content-type" content="text/html; charset=windows-1256">
<title>Maaking.Com File Uploader</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<script language="JavaScript" type="text/javascript">
<!-- ;
var newwindow;
var wheight = 0, wwidth = 0;
function popimg(url, title, iwidth, iheight, colour) {
var pwidth, pheight;

if ( !newwindow || newwindow.closed ) {
pwidth=iwidth+30;
pheight=iheight+30;
newwindow=window.open('','htmlname','width=' + pwidth +',height=' +pheight + ',resizable=1,top=50,left=10');
wheight=iheight;
wwidth=iwidth;
}

if (wheight!=iheight || wwidth!=iwidth ) {
pwidth=iwidth+30;
pheight=iheight+60;
newwindow.resizeTo(pwidth, pheight);
wheight=iheight;
wwidth=iwidth;
}

newwindow.document.clear();
newwindow.focus();
newwindow.document.writeln('<html> <head> <title>' + title + '<\/title> <\/head> <body bgcolor= \"' + colour + '\"> <center>');
newwindow.document.writeln('<a title="Hit to close!" href="javascript:window.close();"><img src=' + url + ' border=0></a>');
newwindow.document.writeln('<\/center> <\/body> <\/html>');
newwindow.document.close();
newwindow.focus();
}

// Routines to tidy up popup windows when page is left
// Call with an onUnload="tidy5()" in body tag

function tidy5() {
if (newwindow && !newwindow.closed) { newwindow.close(); }
}

</script>

<body bgcolor="#ECF5FF" text="#3399FF" leftmargin="20" topmargin="10" marginwidth="10" marginheight="10">

<?php
/*=========================================\
Author      :  Mohammed Ahmed(M@@king)    \\
Version    :  1.1                        \\
Date Created:  Oct 11  2004              \\
----------------------------              \\
Last Update:  August 21 2005            \\
----------------------------              \\
Country    :  Palestine                  \\
City      :  Gaza                      \\
E-mail    :  m@maaking.com              \\
MSN        :  m@maaking.com              \\
AOL-IM    :  maa2pal                    \\
WWW        :  http://www.maaking.com    \\
Mobile/SMS :  00972-599-622235          \\
                                          \\
===========================================\
---Description -----------------------------------------------------
The Super Global Variable $_FILES is used in PHP 4.x.x.
$_FILES['filetoupload']['size'] ==> Get the Size of the File in Bytes.
$_FILES['filetoupload']['tmp_name'] ==> Returns the Temporary Name of the File.
$_FILES['filetoupload']['name'] ==> Returns the Actual Name of the File.
$_FILES['filetoupload']['type'] ==> Returns the Type of the File.

So if I uploaded the file 'test.doc', the $_FILES['filetoupload']['name']
would be 'phptut.doc' and $_FILES['filetoupload']['type'] would be 'application/msword'.
---------------------------------------------------------------------*/

//**********************************************************************//
//  $_FILES['filetoupload']  is the value of                            //
// file field from the form. <input type="file" name="filetoupload">    //
//**********************************************************************//
################################################################################
##---------------------------1 - Setup
################################################################################
  // this is the upload dir where files will go.
  //Don't remove the /
  //Chmod it (777)
  $upload_dir = "billeder/";  //change to whatever you want.
  //51200 bytes = 50KB
  $size_bytes = 1024000; //File Size in bytes (change this value to fit your need)
  $extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
  $limitedext = array(".gif",".jpg",".png",".jpeg"); //Extensions you want files uploaded limited to. also you can use:  //array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe");
################################################################################
##---------------------------2 - check for directory and writable
################################################################################
  //check if the directory exists or not.
  if (!is_dir("$upload_dir")) {
      die ("Error: The directory <b>($upload_dir)</b> doesn't exist");
  }
  //check if the directory is writable.
  if (!is_writeable("$upload_dir")){
      die ("Error: The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
  }

################################################################################
##---------------------------3-1 - code begins here
################################################################################
  if(isset($_POST['uploadform'])){
  // if you clicked the (Upload File) button. "If you submitted the form" then upload the file.
  //begin of uploadform.

          // $filename will hold the value of the file name submetted from the form.
          $file_tmp = $_FILES['filetoupload']['tmp_name'];
          $file_name = $_FILES['filetoupload']['name'];
          //Get the Size of the File
          $file_size = $_FILES['filetoupload']['size'];
         
          //check if no file selected.
          if (!is_uploaded_file($file_tmp)){
          echo "Error: Please select a file to upload!. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit(); //exit the script and don't do anything else.
          }


          //Make sure that file size is correct
          if ($file_size > $size_bytes){
              echo "Error: File Too Large. File must be <b>". $size_bytes / 1024 ."</b> KB. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
              exit();
          }
          //check file extension
          $ext = strrchr($file_name,'.');
          if (($extlimit == "yes") && (!in_array(strtolower($ext),$limitedext))) {
              echo("Error: Wrong file extension. ");
              exit();
          }


          // Check if file is Already EXISTS.
          if(file_exists($upload_dir.$file_name)){
              echo "Oops! The file named <b>$file_name</b> already exists. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
              exit();
          }

          //to remove spaces from file name we have to replace it with "_".
          $file_name = str_replace(' ', '_', $file_name);
          //Move the File to the Directory of your choice
          //move_uploaded_file('filename','destination') Moves afile to a new location.
          if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
                //tell the user that the file has been uploaded and make him alink.
                echo "File (<a href=\"$upload_dir$file_name\">$file_name</a>) uploaded! <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                exit();
          }else{
                // print error if there was a problem moving file.
                echo "There was a problem moving your file. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                exit();
          }

################################################################################
##---------------------------3-2 - code to display the form
################################################################################
  //end of if($uploadform).
  // If the form has not been submitted, display it!
  }else{
  //begin of else

        // get the all extensions from the array
        for($i=0;$i<count($limitedext);$i++){
    if (($i<>count($limitedext)-1))$commas=", ";else $commas="";
list($key,$value)=each($limitedext);
$all_ext .= $value.$commas;
}
 
        //print the form
        echo "<br><h3>::Browse a File to Upload:</h3>"
            ."- Allowed Extensions: $all_ext</b> <br>"
            ."- Max File Size = ". $size_bytes / 1024 ."KB"
            ."<form method=\"post\" enctype=\"multipart/form-data\" action=\"$PHP_SELF\">"
            ."<input type=\"file\" name=\"filetoupload\"><br>"
            ."<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$size_bytes\"><br>"
            ."<input type=\"Submit\" name=\"uploadform\" value=\"Upload billedet!\">"
            ."</form>";
  }//end of else

################################################################################
##---------------------Function to delete files ------------------------------##
################################################################################
 

  function del(){
      // if we want to insert a value inside any function we have to global it first.
global $upload_dir, $file;
$file = $_REQUEST["file"];

      //delete the file
      @unlink($upload_dir."/$file");
      echo "<h3><font color=red>The File ($file) was deleted! <br> Please wait...</font></h3>";
      echo "<meta http-equiv=Refresh content=1;url=upload.php>";
  }

  //define variable to switch between functions
    if( $action == del ){
      del();
  }


//  Here is the most interesting part.
################################################################################
##---------------------------------Added Option ------------------------------##
################################################################################
################################################################################
##---------------------View uploaded files (photo gallery) -------------------##
################################################################################


echo "<br><hr><center><b>Current Uploaded Files (Gallery)</b></center><br>";

// set number of columns  for the Gallery.
$cols = 3;
// set aloop for defining columns.
$i =1;
// creating table inorder to use it for columns.
echo "<table width=100% border ='1' bordercolor='#3399FF' cellpadding='10' cellspacing='6'>
      <tr>";

//open the dir where files were uploaded inside.
$opendir =opendir($upload_dir);
// create a loop and define a variable called ($file)
// to hold the value of file name located in the directory .
while ($file = readdir($opendir)) {
        //exclude anything that doen't look like files.
        if($file != '..' && $file !='.' && $file !=''){
                //dont't read direcories insde our directory.
                if (!is_dir($file)){

                        // since i am using gallery we have to get the width and height of photos.
                        $imgsize = getimagesize ($upload_dir."".$file);

                        // lets get the file size in byte/kb/mb.
                        $file_size = filesize($upload_dir."".$file);

        if ($file_size >= 1048576){
      $show_filesize = number_format(($file_size / 1048576),2) . " MB";
        }elseif ($file_size >= 1024){
      $show_filesize = number_format(($file_size / 1024),2) . " KB";
        }elseif ($file_size >= 0){
      $show_filesize = $file_size . " bytes";
        }else{
      $show_filesize = "0 bytes";
                        }
                       
                        //Gets file modification time
                        $last_modified = date ("F d Y H:i:s", filemtime($upload_dir."".$file));

                        //for images width.
                        if ($imgsize[0] > 100){
                              $base_img = "<img src=\"$upload_dir$file\" border=\"0\" width=\"100\">";
                        }else{
                              $base_img = "<img src=\"$upload_dir$file\" border=\"0\">";
                        }
                        //define a variable to hold everthing that will be printed in table's columns.
                        $all_stuff =  "File name:<b> $file</b> <hr size=1>
                                      <div style=\"width: 120px; height: 120px; z-index: 1; float: left; border: 1px dotted #C0C0C0\">
                                      <a href=\"javascript:popimg('$upload_dir$file','$file', $imgsize[0], $imgsize[1],'white')\">
                                      $base_img</a>
                                      </div>
                                      <p align=left>
                                      Size: $show_filesize<br>
                                      Options: [<a title=\"Delete File\" href=\"javascript:;\" onClick=\"cf=confirm('Are you sure you want to delete?');if (cf)window.location='upload.php?action=del&file=$file'; return false;\">Del</a>]
                                      <br>Width : $imgsize[0] px
                                      <br>Height : $imgsize[1] px
                                      <br>Date added: $last_modified GMT";


                        //divide the loop ($i) and ($cols) if the result is integer
                        //then print column number 1.
                        if (is_int($i / $cols)){
                          echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td></tr><tr>";
                        }else{
                              //else then print column number 2 etc for the next loop.
                              echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td>";
                        }
                        //add only 1 for the loop ($i).
                        $i++;
                }
}
}//end while loop
closedir($opendir);
clearstatcache();
// print the rest of table.
echo "</tr>
      </table>";

//print copyright ;)
echo"<p align=\"right\"><br>Script by: <a href=\"http://www.maaking.com\">maaking.com</a></p>
</body>
</html>";

?>
[/code]

Thanks a lot!
Savi
Link to comment
Share on other sites

Are the folder permissions set correctly?
Whats the error you are getting?
If it is permissions
The CHMOD of the folder should be 0755
or this dont work try 777

Also, you should not use unlink of a folder using request, its easy hack
I could just come along and type
file.php?action=del&file=file.php

Now you have had it aint ya
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.