Jump to content

Recommended Posts

Hello, let me start by saying I am extremely new. I appreciate the PHP/Mysql help but sometimes help is always assumed that I already know something else first. So I always feel like something is missing in the explanation.

 

I have gotten as far as to getting a file uploaded but have no idea how the rename the file. I have built a ticket tracking system and I would like to include file attachments for each ticket. Only I have no idea how to uniquely name the file and some how make a link to it within the ticket.  Some how I need to have it so that the names are unique so the files can never get overwritten.

 

Preferably, I like the file named by username and ticket number. I.E. username_ticketnumber.extension

 

Help???  Thanks!

Link to comment
https://forums.phpfreaks.com/topic/100506-upload-file-using-variable-as-file-name/
Share on other sites

To give it a unique name you could prepend it with data from uniqid(). Store the path to the file and the filename it should have in a database. Then you can access it like download.php?file=213 with a query like this:

SELECT * FROM files WHERE file_id = 213;

 

In the table you could assign the file with a ticket and a user as well by referencing the ticket id and the user id it should be assigned to.

There are a couple ways you can do this. Once the ticket has been inserted you can have mysql return the unique id of the ticket.

 

$uniqueID = mysql_insert_id();

since the form was just submitted you have the name of the person who has submitted it.

 

So that being said you could rename the file like this

$name = $_POST['namefield'];
$uniqueID = mysql_insert_id();
$ext = strrchr($_FILES['file']['name'], ".");
$tmp_file = $_FILES['file']['tmp_name'];
$new_name = $username."_".$uniqueID.$ext;
move_uploaded_file($tmp_file, $new_name);

 

Where $_FILES['file']['tmp_name'] replace ['file'] with the name you use on your form for the file.

 

Ray

Thanks for the info. It still sounds a little foreign to me. This is what I have in the page that my input form is in. This works to upload the file but still not sure how to integrate your code to insert the link into the database. Also, I grabed this code from a few sites and stitched it together. Only I cannot figure out how to remove the limitation it has with only uploading images. I'd like to get it to upload any file.

 

<?php
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 "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>

<?php
   if ((($_FILES["file"]["type"] == "image/gif")
   || ($_FILES["file"]["type"] == "image/jpeg")
   || ($_FILES["file"]["type"] == "image/pjpeg"))
   && ($_FILES["file"]["size"] < 20480))
  {
  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"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
//  echo "Invalid file";
  }
?>

 

Edited by Daniel0 to have

 tags around the code.[/i]

Here is some code for you to chew on :)

 

<?php
// check to see if form has been submitted
if(isset($_POST['submit'])){
// code to display form below

} else {
// Code to connect to your database here


// upload file and insert data
  if ($_FILES["file"]["size"] > 204800 && $_FILES["file"]["error"] > 0){
  echo "File is too Large or There is an error in your upload<br />
      Return Code: " . $_FILES["file"]["error"] . "<br />";
  } else {
  $sql =  "INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES ('value1', 'value2', 'value3')";
  $res = @mysql_query($sql);
    if(!$res){
    echo "could not insert your ticket and no files have been uploaded<br />
        SQL Error: ".die(mysql_error());
    } 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 />";
    $name = $_POST['namefield'];
    $uniqueID = mysql_insert_id();
    $ext = strrchr($_FILES['file']['name'], ".");
    $new_name = $username."_".$uniqueID.$ext;
    move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $new_name);
    echo "Stored in: " . "uploads/" . $new_name;
    }
  }
}
?>

 

Also not sure why it looks like you have the same code twice above. Also Is there a need to store the file name in the DB? You will know the name of the file later just be combining the username and the id together, so is it really necessary to store the name of the file?? If you do want to store it let me know, you will have to run another query to do it.

 

Ray

I guess I should explain what I'm trying to accomplish.

 

I want to display within a ticket a hyperlink that pulls the file from the network. Thats the bottom line. So some how I need my DB to know where the file is and have a \\network\path\to\thefile.txt to be inserted into a field within the ticket's record. Unless there is another or easier way? (:

 

Also want to add that I'm using dreamweaver cs3 to create my inserts, etc.. maybe feature you are aware of that can assist?

well once you insert the support ticket you can update the row with the file name.

 

    move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $new_name);
mysql_query("UPDATE `table` SET `filefield` = '$new_name' WHERE `support_id` = '$uniqueID'") or die(mysql_error());

 

you can also change $file_name to the entire link if you want.

 

Ray

well when the user is viewing the support ticket you will query the database. so you can do something like

 

echo "<a href=\"uploads/files/".$r['filefield']."\" />".$r['filefield']."</a> is the support file that has been uploaded";

 

So now the link is static and just the file is put in. I think that is what you want.

 

Ray

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.