Stavros Posted December 22, 2010 Share Posted December 22, 2010 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 More sharing options...
denno020 Posted December 22, 2010 Share Posted December 22, 2010 http://php.net/manual/en/function.rename.php Can you also make use of the tags when posting code. Makes it much easier to read. Denno Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150155 Share on other sites More sharing options...
Stavros Posted December 22, 2010 Author Share Posted December 22, 2010 Thanks for the link but couldn't see anything regarding adding time stamp to file? Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150180 Share on other sites More sharing options...
denno020 Posted December 22, 2010 Share Posted December 22, 2010 You can pull the time from the server and save it into a variable. Investigate if you can then put that variable into the file name, similar to an echo. Denno Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150181 Share on other sites More sharing options...
Stavros Posted December 22, 2010 Author Share Posted December 22, 2010 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; } ?> Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150191 Share on other sites More sharing options...
QuickOldCar Posted December 22, 2010 Share Posted December 22, 2010 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(); ?> Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150206 Share on other sites More sharing options...
QuickOldCar Posted December 22, 2010 Share Posted December 22, 2010 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(); ?> Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150207 Share on other sites More sharing options...
denno020 Posted December 22, 2010 Share Posted December 22, 2010 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 Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150317 Share on other sites More sharing options...
Stavros Posted December 22, 2010 Author Share Posted December 22, 2010 Great thanks for al the help here, yes the insert was part of an update "message board" I was creating. I will give the new code a go and try and understand it as well. cheers! Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150490 Share on other sites More sharing options...
Stavros Posted December 22, 2010 Author Share Posted December 22, 2010 Awesome it worked! Link to comment https://forums.phpfreaks.com/topic/222350-rename-file-while-uploading/#findComment-1150518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.