Jump to content

Recommended Posts

Hi all,

 

i have a form, which can upload photos to the database.

This is working and the images are being saved in the images folder of my database, no problems there.

What i am attempting to do, and havin difficulty doing so is two things:

 

a) as there will be multiple users of the form uploadin photos, i do not want images to be saved in the database with the same name, and lookin for code that will prevent users from uploadin an image with the same name as one that is already saved in the database.

 

b)secondly, i am looking for some code which will only allow .jpg, .gif, and .pgn files to be uploaded, preventing the user from uploading .doc etc....

 

here is the code i have so far

 

any suggestions would be greatly appreciated!

 

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);

// Connects to your Database
mysql_connect("db detals ", "db details ", " db details ") or die(mysql_error()) ;
mysql_select_db(" db details") or die(mysql_error()) ;


//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/171634-duplicate-image-name-problem/
Share on other sites

To get around the duplicate image name problem, control the image names yourself. You could either a have unique string as the full name, or attach it to the end of the original image, e.g. 1251120114.jpg ... or ... my_picture_2009-1251120114.jpg. A good way to ensure (as good as) a completely unique number is to attach a random number to time.

 

As for checking the file extension, look around on Google. There's literally millions of tutorials out there for uploading images with PHP, I'm sure with a bit of work you'll be able to implement some of the code used.

Thanks  for that!

 

i've messed around a bit further with the code, and have put bits and pieces together.

however, one or two things still not correct.

any more suggestions would again be greatly appreciated...

 

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);

// Connects to your Database
mysql_connect("db deatils", "db details", "db details") or die(mysql_error()) ;
mysql_select_db("db details") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ;

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

?> 

What are the one or two things not correct? Or.. what errors / messages are you getting back?

 

This is the kind of thing I meant before...

 

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"] . "-" . time());

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.