Lamez Posted March 22, 2008 Share Posted March 22, 2008 How would I be able to use the random function to rename a file? I have this <?php $filename = $_FILES["file"]["name"]; $newname = rand(100, 9000000); $new_filename = rename($filename, $newname["name"]); $final = md5($new_filename); ?> but I get this error: Warning: rename(bb.png,) [function.rename]: No such file or directory in /mounted-storage/home48c/sub007/sc33591-LWQU/lamezz.info/user/usrava/new_upload.php on line 32 Uploaded what am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/ Share on other sites More sharing options...
Barand Posted March 22, 2008 Share Posted March 22, 2008 what am I doing wrong? Everything 1 ) First you need a physical file "bb.png" to rename. At the moment all you have is its name from the FILES array and a temprorary uploaded file. First move the uploaded temp file to $filename on your server www.php.net/move_uploaded_file 2 ) What are you expecting in $newname["name"] ? 3 ) $new_filename will just contain either TRUE or FALSE www.php.net/rename Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/#findComment-498099 Share on other sites More sharing options...
Lamez Posted March 22, 2008 Author Share Posted March 22, 2008 My script, I just wrote is suppose to check to see if the filename is a image, then it checks for spaces, if so replace them with _, then it is SUPPOSE to upload the file, then it generates a random number, renames that uploaded image with the new number, user, and old filename, then it takes that as a string and makes a hash encryption out of it, with the user, then it ends. Well it does not upload the file! Why? here is the code: <?php include ("../../style/include/session.php"); $user = $session->username; $uploaddir = '/'; ?> <html> <body> <form method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file" /> <br/> <input type="submit" name="upload" value="Upload" /> </form> </body> </html> <?php if (isset($_POST['upload'])){ if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/bmp") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 104857600)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { $bfilename = $_FILES["file"]["name"]; $filename = str_replace(' ', '_', $bfilename); $uploaddir . basename($filename); $newfile = $uploaddir . "/$filename"; $tmp = $_FILES["file"]["tmp_name"]; move_uploaded_file($uploaddir, $tmp); $newname = rand(100, 9000000); $new_filename = rename($filename, $newname."-".$user."-".$filename); $final = md5($new_filename)."-".$user.".".getExt($filename); echo "<br>"; echo "Before Name: ".$bfilename; echo "<br>"; echo "Random Number: ".$newname; echo "<br>"; echo "After Name: ".$new_filename; echo "<br>"; echo "Hased Name: ".$final; echo "<br>Uploaded"; } } else { echo "Invalid file"; } } function getExt($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/#findComment-498101 Share on other sites More sharing options...
Barand Posted March 22, 2008 Share Posted March 22, 2008 How many threads are you planning to start on this this problem? EDIT: Threads now merged Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/#findComment-498105 Share on other sites More sharing options...
Lamez Posted March 22, 2008 Author Share Posted March 22, 2008 I keep switching my code, and I do not want to get confused, I understand if you have to delete this, and my other threads. sorry Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/#findComment-498106 Share on other sites More sharing options...
Barand Posted March 22, 2008 Share Posted March 22, 2008 Some errors here <?php $bfilename = $_FILES["file"]["name"]; $filename = str_replace(' ', '_', $bfilename); $uploaddir . basename($filename); // $_FILES[file][name] already contains basename only $newfile = $uploaddir . "/$filename"; // newfile now contains "//filename" !!! $tmp = $_FILES["file"]["tmp_name"]; move_uploaded_file($uploaddir, $tmp); // arguments are wrong way round uploaddir should be newfile !!! ?> Also see my previous reply regarding rename() Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/#findComment-498109 Share on other sites More sharing options...
Lamez Posted March 22, 2008 Author Share Posted March 22, 2008 alright, it is still not uploading, but would this work, if the file was uploaded? <?php { $bfilename = $_FILES["file"]["name"]; $filename = str_replace(' ', '_', $bfilename); $uploaddir . basename($filename); $tmp = $_FILES["file"]["tmp_name"]; move_uploaded_file($uploaddir, $filename); $newname = rand(100, 9000000); $new_filename = rename($filename, $newname."-".$user."-".$filename); $final = md5($new_filename)."-".$user.".".getExt($filename); echo "<br>"; echo "Before Name: ".$bfilename; echo "<br>"; echo "Random Number: ".$newname; echo "<br>"; echo "After Name: ".$new_filename; echo "<br>"; echo "Hased Name: ".$final; echo "<br>Uploaded"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/#findComment-498378 Share on other sites More sharing options...
Barand Posted March 22, 2008 Share Posted March 22, 2008 No. Guess you ignored all my comments. Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/#findComment-498392 Share on other sites More sharing options...
Lamez Posted March 22, 2008 Author Share Posted March 22, 2008 No. Guess you ignored all my comments. no I did not ignore them, I thought I fixed them, let me look over them again. Quote Link to comment https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/#findComment-498404 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.