lip9000 Posted December 5, 2007 Share Posted December 5, 2007 I have a script that generates a random number for the name of a file that has just been uploaded, and I would like to get this number that has just been created and send it in a URL varible into the next page whereby it gets inserted into the database. Here is the script: <?php $ran = rand(1000000, 9000000000); $ran2 = $ran."."; $uploadFile = "tutorials/"; $uploadFile = $uploadFile . $ran2.$ext; ?> window.location = "addupload.php?tid=<?php echo $row_rsTutorials['tutorialID']; ?>&name=<?php echo $uploadFile; ?>" The script works, however the value that gets passed in the URL varible "name" is a different value to the random number that was generated for the file name. The script is getting run again when it is being echoed, this a completely different random number is being generated. I want to pass through the first random number that was generated for the file name, not a completely different one. Any ideas? Many thanks in advance. Quote Link to comment Share on other sites More sharing options...
trq Posted December 5, 2007 Share Posted December 5, 2007 The code you posted can not possibly do what your saying it does. The random number is generated once and once only in that small snippet. The link isn't pointing to itself per chance? Quote Link to comment Share on other sites More sharing options...
lip9000 Posted December 5, 2007 Author Share Posted December 5, 2007 The script I've posted isn't the file upload as well, it is only the snippet of the random number generator. The variable $uploadFile contains the filename of the uploaded file, which is a random generated number, for example 8304382583.avi would be the name of the file. When I upload the file, I see the file 8304382583.avi (or whatever number gets randomly generated) get's placed in the uploads folder, however if I echo the same variable, being $uploadFile, onto the document, it comes up with a different random number. So if I echo $uploadFile again, the random number generating script is being run again and is spitting out a different number. Instead I want the variable $uploadFile to retain it's current value and never change, so that I can pass that into the next page and insert that value into the database. Quote Link to comment Share on other sites More sharing options...
lip9000 Posted December 6, 2007 Author Share Posted December 6, 2007 Any suggestions anyone? ??? Quote Link to comment Share on other sites More sharing options...
lip9000 Posted December 6, 2007 Author Share Posted December 6, 2007 Is there a way you could perhaps store that random number value into a txt file, then get the value from the txt file, that way it doesn't execute the random number generating code again??? Quote Link to comment Share on other sites More sharing options...
trq Posted December 6, 2007 Share Posted December 6, 2007 I'll say it again. The code you have posted does not (allone) do what you say. PHP runs from top to bottom, so the random number part will never be re-executed. You need to provide more code and a clearer description of what you are doing. Quote Link to comment Share on other sites More sharing options...
lip9000 Posted December 6, 2007 Author Share Posted December 6, 2007 Ok, here's the whole script. <?php $uploadDir = dirname(__FILE__) . '/tutorials/'; $uploadFile = $uploadDir . basename($_FILES['Filedata']['name']); //This function separates the extension from the rest of the file name and returns it function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } //This applies the function to our file $ext = findexts ($_FILES['Filedata']['name']) ; //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. $ran = rand(1000000, 9000000000); //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended. $ran2 = $ran."."; //This assigns the subdirectory you want to save into... make sure it exists! $uploadFile = "tutorials/"; //This combines the directory, the random file name, and the extension $uploadFile = $uploadFile . $ran2.$ext; if ($_POST['submit'] != '') { // 1. submitting the html form if (!isset($_GET['jqUploader'])) { // 1.a javascript off, we need to upload the file if (move_uploaded_file ($_FILES[0]['tmp_name'], $uploadFile)) { // delete the file // @unlink ($uploadFile); $html_body = '<h1>File successfully uploaded!</h1><pre>'; $html_body .= print_r($_FILES, true); $html_body .= '</pre>'; } else { $html_body = '<h1>File upload error!</h1>'; switch ($_FILES[0]['error']) { case 1: $html_body .= 'The file is bigger than this PHP installation allows'; break; case 2: $html_body .= 'The file is bigger than this form allows'; break; case 3: $html_body .= 'Only part of the file was uploaded'; break; case 4: $html_body .= 'No file was uploaded'; break; default: $html_body .= 'unknown errror'; } $html_body .= 'File data received: <pre>'; $html_body .= print_r($_FILES, true); $html_body .= '</pre>'; } $html_body = '<h1>Full form</h1><pre>'; $html_body .= print_r($_POST, true); $html_body .= '</pre>'; } else { // 1.b javascript on, so the file has been uploaded and its filename is in the POST array $html_body = '<h1>Form posted!</h1><p>Error:<pre>'; $html_body .= print_r($_POST, false); $html_body .= '</pre>'; } // myHtml($html_body); } else { if ($_GET['jqUploader'] == 1) { // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 2. performing jqUploader flash upload if ($_FILES['Filedata']['name']) { if (move_uploaded_file ($_FILES['Filedata']['tmp_name'], $uploadFile)) { // delete the file // @unlink ($uploadFile); return $uploadFile; } } else { if ($_FILES['Filedata']['error']) { return $_FILES['Filedata']['error']; } } } } // /////////////////// HELPER FUNCTIONS mysql_free_result($rsTutorials); ?> <script type="text/javascript"> <!-- window.location = "addupload.php?tutorialname=<?php echo $uploadFile; ?>" //--> </script> The last bit of code in the javascript, it get's the value of $uploadFile again and sends it to the next page, but echoing the value of $uploadFile executes the random number generating code again which is in $ran, thus spitting out a different number. I don't want the code to execute again, I only want it to execute once so I can get the name of the file. Quote Link to comment Share on other sites More sharing options...
trq Posted December 6, 2007 Share Posted December 6, 2007 echoing the value of $uploadFile executes the random number generating code again Thats what you think, fortunately however, that is NOT possible. Quote Link to comment Share on other sites More sharing options...
lip9000 Posted December 6, 2007 Author Share Posted December 6, 2007 LOL What do you mean not possible!!! It's happening! I'm going to make you a video to prove it. Quote Link to comment Share on other sites More sharing options...
trq Posted December 6, 2007 Share Posted December 6, 2007 It might appear to be happening, but not with the code you provided. Or not that code allone. Explain to me how then line 31 can be executed again when the script gets to the bottom simply by calling a variable. Do yourself a favour and everywhere in this script that you set $uploadFile = whatever, place.... echo $uploadFile; below it. Then replace.... <script type="text/javascript"> <!-- window.location = "addupload.php?tutorialname=<?php echo $uploadFile; ?>" //--> </script> with the same. Show us the output. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.