Jump to content

Image Upload/Rename


Dave2711

Recommended Posts

Hi, im trying to make an image upload script which will rename the images to something unique to my uploads folder, and post the link of the image just below on the page.. A friend of mine sent me their image upload script which ive editted slightly but still cannot figure out how to force name changes for each file, and I have a feeling he deleted aload of stuff out of it but left some there so its a fairly messy script as it is.. Any chance anyone could help?

 

The script is as follows:

<?php

//Max file size (Current 6.3mb)

$MAX_SIZE = 6300000;

                           

//Allow these extensions         

$FILE_EXTS  = array('.jpg','.png','.gif');

 

//Allow file delete? no, if only allow upload only

$DELETABLE  = false;                             

 

 

 

$site_name = $_SERVER['HTTP_HOST'];

$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);

$url_this =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

 

$upload_dir = "uploads/";

$upload_url = $url_dir."/uploads/";

$message ="";

 

 

/************************************************************

*    Process User's Request

************************************************************/

if ($_FILES['userfile']) {

  $resource = fopen("log.txt","a");

  fwrite($resource,date("Ymd h:i:s")." - $_SERVER[REMOTE_ADDR] - "

            .$_FILES['userfile']['name']." "

            .$_FILES['userfile']['type']."\n");

  fclose($resource);

 

  $file_type = $_FILES['userfile']['type'];

  $file_name = $_FILES['userfile']['name'];

  $file_ext = strtolower(substr($file_name,strrpos($file_name,".")));

 

  //File Size Check

  if ( $_FILES['userfile']['size'] > $MAX_SIZE)

    $message = "The file size is over 6MB.";

  //File Extension Check

  else if (!in_array($file_ext, $FILE_EXTS))

    $message = "$file_name($file_type) is not a valid format.";

  else

    $message = do_upload($upload_dir, $upload_url);

 

  print "<script>window.location.href='$url_this?message=$message'</script>";

}

else if (!$_FILES['userfile']);

else

$message = "Specified file does not exist, or is incorrect format.";

 

 

 

$handle=opendir($upload_dir);

$filelist = "";

while ($file = readdir($handle)) {

  if(!is_dir($file) && !is_link($file)) {

      $filelist .= "<a href='$upload_dir$file'>".$file."</a> - URL: <b>$upload_url$file</b>";

      if ($DELETABLE)

       

      $filelist .= " Added at  ".date("d-m H:i", filemtime($upload_dir.$file))

                  ."";

$filelist .= " <a style='text-decoration:none; font-weight:bold'  href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a>";

      $filelist .="<br>";

  }

}

 

function do_upload($upload_dir, $upload_url) {

 

$temp_name = $_FILES['userfile']['tmp_name'];

$file_name = $_FILES['userfile']['name'];

  $file_name = str_replace("\\","",$file_name);

  $file_name = str_replace("'","",$file_name);

$file_path = $upload_dir.$file_name;

 

//File Name Check

  if ( $file_name =="") {

  $message = "Invalid File Name Specified";

  return $message;

  }

 

  $result  =  move_uploaded_file($temp_name, $file_path);

  if (!chmod($file_path,0777))

  $message = "Destination Folder does not exist, please contact Administator.";

  else

    $message = ($result)?"$file_name was uploaded successfully.<br>Direct URL: http://bla.com/$file_name" :

          "Upload Failed.";

  return $message;

}

 

?>

 

 

<html>

<head>

<title>Page Title</title>

<link rel=stylesheet href="style.css">

</head>

<body>

<br><br>

<center>

  <br>

Free <b>.JPG</b>, <b>.GIF</b>, <b>.PNG</b> file hosting.

  <br>

  <form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">

    Upload File: <input type="file" id="userfile" name="userfile">

    <input type="submit" name="upload" value="Upload">

  </form>

 

<br>

<b><u>Upload Details:</b></u>

<br><br>

 

<font color=red><?=$_REQUEST[message]?></font>

 

 

</center>

 

If anyone can help tidy some of it up ill be grateful too.. as I was hoping to learn something from it but as it is I dont understand half of it.

 

Thanks very much, Dave

Link to comment
https://forums.phpfreaks.com/topic/167395-image-uploadrename/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.