I have a file upload script that works just fine, however, I also need to extract the filename from the upload script, so I can store the name in a database for usage later on. After the upload form, and before processing, I also append a variable name to the front of the file, to name the file with the associated project. As I said, the script works just as it should, the files are uploaded and renamed correctly. Her is the form:

$project=$_SESSION['project'];
		echo '<span class="route"><h3><a href="index.php?select=mstudio" target ="_self">Studios</a> ';
		echo ' - <a href="index.php?studio=' . $studio . '" target ="_self">' . $studio . ' </a> - ' . $project . '</h3></span>';
//		echo 'Add a new track to the project ' . $project;
		echo '<form enctype="multipart/form-data" action="index.php" method="POST">';
		echo '<input type="hidden" name="trackupload" value="yes">';		
		echo '<input type="hidden" name="MAX_FILE_SIZE" value="10000000">';
		echo '<table>';
		echo '<tr>';
		echo '<td>Please select a track to UpLoad</td>';
		echo '</tr>';
		echo '<tr>';
		echo '<td><input name="uploaded" type="file" size ="60"/></td>';
		echo '</tr>';
		echo '<tr>';
		echo '<td><input type="submit" value="Add Track!"><input type="reset"></td>';
		echo '</tr>';
		echo '</table>';
		echo '</form>';

Here is the processing portion:

$project=$_SESSION['project'];		
		$track=basename( $_FILES['uploadedfile']['name']);
// Where the file is going to be placed 
		$target = "studios/";
		$target = $target . $project . '_' . basename( $_FILES['uploaded']['name']) ;
		if (file_exists($target))
			{
				echo ' DUPLICATE FILE!!!!';
			}
		$ok=1;
//		$track=basename( $_FILES['uploadedfile']['name']);
		if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
			{
				echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
			}	
			else 
			{
				echo mysql_error();
			}
			$pname=$_SESSION['project'];
			$uname=$_SESSION['user'];
			$tdate=date("y-m-d  H:i:s", time());
			echo $pname, $uname, $track, $tdate;

The last echo statement is so I can see if it is working, the $track is the variable that is supposed to reflect the uploaded file name. Why is it not working? I must be not extracting the filename correctly to assign it to a variable, however, the file is uploaded correctly.

Ice

    $project in your form has its value in session variable. you don't need to pass with a URL variable.

    if the move_upload has failed why do you want to print the mysql_error() ?

    The mysql and upload (permits) errors not in an actual relation.

    to know that all your variables alive,
    test their existings, like:

    if(empty($_SESSION["project"]))
    {
    die("Project is empty!");
    }

    if you store the file names in a database, it time to change this technique to rename the uploaded files to the created mysql row's primary number to protect special charaters, such as white space in your date!, and other special characters such as ":" ???

    You should filter the file duplication on a mysql side (set the filename and uploader to unique key , and on upload test the mysql_errno() 's value.)

    To resume these things:
    save the project's name , inserted date-time, and other meta data into the mysql table.

      Ok, let me clarify things a little bit for you. First, the $project is not the file name, nor is it the issue. It is not the variable. It is the relationship of what the uploaded file is associated with. Secondly, the $project comes form a session variable because it is kept alive across different pages, and is what the user is dealing with at any given time, in this part of the website. And where do you see that it is coming from a passed URL variable? The object I am dealing with is passed thru a $_POST variable from a form. It is a file name, not the $project name.

      As for why I want to print the MySql error, it is both for troubleshooting purposes and to let the user know there was an error on the upload. However, mostly for troubleshooting on the MySql error, as, this is a developing script still, and I want to know what issues I am running into. On the other hand, if there is an error, I need to let the user know that there was an error and tell them why. I see your point about the relation of the mysql and upload errors, but I still need my own indications of errors. And yes, I also have a clause in there to indicate an upload error. Your message of

      if(empty($_SESSION["project"]))
      {
      die("Project is empty!");
      } 

      has nothing to do with any problem, as $project is not even in the picture, it is something else entirely and it has no issues.

      As for

      if you store the file names in a database, it time to change this technique to rename the uploaded files to the created mysql row's primary number to protect special characters, such as white space in your date!, and other special characters such as ":" ???

      , the filename is the basename of the uploaded file with the project name appended to the front of it with an underscore in between. This is how the file is actually stored in the file system, and how the files are seperated from each other. There could be fifteen files called 'bass.mp3', but only one will be associated with the project, thusly, the file would be called 'myproject_bass.mp3', no matter what the file name that they upload.

      So, I can see your possible confusion as to what is needed here, as you were addressing the issue of the $project being what is needing to be addressed. However, this is not the case. To sum up, as I said, the script works fine, the file is uploaded and renamed and saved into the directory, however, I still have not received any valid help on how I can retrieve the base filename into a variable to store into the database. The $project is NOT the filename, and it is NOT the issue. And the check for a duplicate occurs on the uploaded file, to see if one with that name already exists, and once again, is completely different from $project. $project can have multiple files associated with it.

      Ice

        How could you save the uploaded file name into a database?
        $target holds the direction to the filesystem in your case.

        When you echo the variables, you wrote $track and not $target.
        Try to replace the comma(,) into point mark(.)

        If you're renaming a file to a selected project name, then you need to rename the project name (its a usually feature) To keep the relation between projects and filenames, are the uploaded filenames renamed too? What about the special characters in the uploaded file names. Its difficult to make urls for special characters. i'm working with sound files, most of the file names have # and ' " characters. Try to build direct links for them.

          Ok, point of confusion on both ends. The $track if you will look at the top of the second script, is my attempt to grab the base file name right away, and hold onto that for a diagnostic display, which you see echo'd out at the bottom of the second script. I wanted to see what was coming out of that, as I was not getting any file name result back from

          echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";

          , so , I tried to capture the file name at the beginning, before doing any modifications to anything, so that I could see what file name was being handed over from the previous form.

          Now, confusion on my end..:

          Try to replace the comma(,) into point mark(.)

          What comma and period are you talking about? And, I still do not have an answer as to where my file name is not coming from. I still can not get the file name itself assigned to a variable.

          The problem is with the $target variable, the entire path is there, and I need to extract the basename of that path into a different variable.

          Ice

            And once again, the $PROJECT does NOT get renamed, only the uploaded file names are APPENDED to.

              Oh wow. The problem was a simple typo on my part. In the segment of the code where I assigned the $track the basename of the uploaded file, I typed :

              $track=basename( $_FILES['uploadedfile']['name']);

              instead of

              $track=basename( $_FILES['uploaded']['name']);

              , thusly, there was no such thing with the extra word 'file' on the variable. How silly of me.

              Now the script works just perfect the way it should, lol. Thanks for the help.

              Ice

                Nice!

                the uploaded file, I typed...

                That's why i like the error reporting ON:

                error_reporting(E_ALL);
                ini_set("display_errors", 1);

                Then you get all undefined variables in your code.

                jjozsi.

                  Write a Reply...