Jump to content

Recommended Posts

Hello I am new to programming, coding, php... and need some help. I have some code that uploads a file and does some basic checks but I also want it to rename the file possibly adding the time stamp.

 

Heres what I have so far.

 

cheers.

 

$tbl_name="guestbook"; // Table name

$email = $_REQUEST["email"];

$name = $_REQUEST["name"];

$comment = $_REQUEST["comment"];

$_FILES["file"]["name"];

$_FILES["file"]["type"];

$_FILES["file"]["size"];

$_FILES["file"]["tmp_name"];

$_FILES["file"]["error"];

$datetime = date("dmy");

$uploaddir = "upload/";

$filename = $_FILES["file"]["name"];

$pathinfo = pathinfo($_FILES['userfile1']['name']);

 

 

mysql_connect("$host", "$username", "$password")or die("cannot connect server ");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";

$result=mysql_query($sql);

 

if($result){

echo "Successful added update to the Melody Bear Bulletin Board";

echo "<BR>";

echo "<a href='ViewBulletinBoard.php'>View Bulletin Board</a>"; }

 

else {

echo "ERROR";

}

 

 

if ((($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/pjpeg"))

&& ($_FILES["file"]["size"] < 2000000))

  {

  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"]. "<br>";

      }

    }

  }

else

  {

  echo "Invalid file";

  }

 

 

 

mysql_close();

?>

 

Link to comment
https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/
Share on other sites

 

would this be on the right track? Then save string as file name?

 

Example #6 Using output buffering to include a PHP file into a string

 

<?php

$string = get_include_contents('somefile.php');

 

function get_include_contents($filename) {

    if (is_file($filename)) {

        ob_start();

        include $filename;

        $contents = ob_get_contents();

        ob_end_clean();

        return $contents;

    }

    return false;

}

 

?>

 

I changed a bit of this for you, one thing I do not understand is why that insert query has nothing to do with the code. That for something else? Are you planning on using the query to save the image locations and just didn't get to that part?

 

<?php
$timestamp = time();
$tbl_name="guestbook"; // Table name
$email = $_REQUEST["email"];
$name = $_REQUEST["name"];
$comment = $_REQUEST["comment"];
$datetime = date("dmy");
$uploaddir = "upload/";
$filename = $timestamp.$_FILES['file']['name'];
$filename = strtolower($filename);
$final_location = $uploaddir$filename;
$pathinfo = pathinfo($_FILES['userfile1']['name']);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $filename . "<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($final_location))
      {
      echo $filename . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      $final_location);
      echo "Stored in: " . $final_location . "<br>";
      
      mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

//not saving the filename to database? wondering why this was here for an image upload form
$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
      }
    }
  }
else
  {
  echo "Invalid file";
  }
   
if($result){
echo "Successful added update to the Melody Bear Bulletin Board";
echo "<BR>";
echo "<a href='ViewBulletinBoard.php'>View Bulletin Board</a>"; }

else {
echo "ERROR";
}   
      
mysql_close();
?>

I wrapped the final_location in quotes.

 

<?php
$timestamp = time();
$tbl_name="guestbook"; // Table name
$email = $_REQUEST["email"];
$name = $_REQUEST["name"];
$comment = $_REQUEST["comment"];
$datetime = date("dmy");
$uploaddir = "upload/";
$filename = $timestamp.$_FILES['file']['name'];
$filename = strtolower($filename);
$final_location = "$uploaddir$filename";
$pathinfo = pathinfo($_FILES['userfile1']['name']);//not sure what doing with this

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $filename . "<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($final_location))
      {
      echo $filename . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      $final_location);
      echo "Stored in: " . $final_location . "<br>";
      
      mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

//not saving the filename to database? wondering why this was here for an image upload form
//I stuck this here if wanted to save the images if image was accepted, if not just move down below
$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
      }
    }
  }
else
  {
  echo "Invalid file";
  }
   
if($result){
echo "Successful added update to the Melody Bear Bulletin Board";
echo "<BR>";
echo "<a href='ViewBulletinBoard.php'>View Bulletin Board</a>"; }

else {
echo "ERROR";
}   
      
mysql_close();
?>

I'm not sure mate, sorry.

 

The only thing I would be doing it googling it for you, which defeats the point. You should just keep searching for how to input variables into filenames, and if you still come up empty, at least you might have more of an idea of what does and doesn't work...

 

Denno

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.