Jump to content

Changing file name on upload


Drongo_III
 Share

Recommended Posts

Hi guys

 

Still learning php and have what is probably quite a newbie question!

 

I have an upload script (below) that uploads images but I can't work out how to change the name of the file before it's moved from it's temporary location to the 'uploads' directory. I've tried a few things but nothing seems to work and I'm a bit stuck.

 

The ultimate goal is to time stamp the new image name so that it will be unique but the first step is to figure out how to change the name.

 

Any help would be much appreciated!

 

Thanks,

 

Drongo

 

The script I'm using is as follows:

 

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000))
  {
  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";
  }

 

Link to comment
Share on other sites

Instead of $_FILES['file']['name'], you can use whatever you want. Take this example:

 

<?php
$file = $_FILES['file']['name'];

//get the file extension, we'll need it to construct the new filename
$ext = pathinfo($file, PATHINFO_EXTENSION);

$new_filename = "any_filename_you_want.$ext";
?>

Link to comment
Share on other sites

Hi Guilty

 

Thanks for such a quick reply.

 

Sorry to labour the point. I've incorporated your code into mine. Is what I've done correct? It works fine but I want to be sure I'm doing it the right way. :)

 

<?php



if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000))
  {
  


$file = $_FILES['file']['name'];

//get the file extension, we'll need it to construct the new filename
$ext = pathinfo($file, PATHINFO_EXTENSION);

$new_filename = "any_filename_you_want2222332.$ext";


      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $new_filename);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];


      }

else
  {
  echo "Invalid file";
  }








// ############ THIS IS THE DATABASE PART ###############



$title = $_POST['title'];

$body = $_POST['body'];



$con = mysql_connect("localhost","zzzzzzzz_testing","RB.x;)c*=a4z");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create table

mysql_select_db("zzzzzzzz_newtest", $con);



mysql_query("INSERT INTO page_stuff (body_text, title, image)
VALUES ( '$body', '$title', '$image_name')");


mysql_close($con);




?> 







 

 

 

Instead of $_FILES['file']['name'], you can use whatever you want. Take this example:

 

<?php
$file = $_FILES['file']['name'];

//get the file extension, we'll need it to construct the new filename
$ext = pathinfo($file, PATHINFO_EXTENSION);

$new_filename = "any_filename_you_want.$ext";
?>

Link to comment
Share on other sites

Exactly! Just that this part:

<?php
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
?>

 

could be put in a conditional, so that you know if the upload works:

<?php
if (move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename)) {
     echo 'Stored in: upload/' . $new_filename;
} else {
     echo 'An error occurred while uploading.';
}
?>

Link to comment
Share on other sites

Fabulous! Thank you so much for your help! It all makes a lot more sense now :)

 

 

Exactly! Just that this part:

<?php
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
?>

 

could be put in a conditional, so that you know if the upload works:

<?php
if (move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename)) {
     echo 'Stored in: upload/' . $new_filename;
} else {
     echo 'An error occurred while uploading.';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

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