dachshund Posted August 16, 2009 Share Posted August 16, 2009 just trying to rename any image uploaded to 'profilepicture.jpg' rename($filename, "profilepicture.jpg"); $newname = implode("/", $parts) . '/images/upload/' . $row['id'] . '/' . $filename; but don't think this is it somehow. Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/ Share on other sites More sharing options...
Andy-H Posted August 16, 2009 Share Posted August 16, 2009 $newname = implode("/", $parts) . '/images/upload/' . $row['id'] . '/profilepicture.jpg'; if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $newname))) { Try that? Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899649 Share on other sites More sharing options...
dachshund Posted August 16, 2009 Author Share Posted August 16, 2009 hmm no. that still keeps the file name as it was. thanks though. Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899651 Share on other sites More sharing options...
Andy-H Posted August 16, 2009 Share Posted August 16, 2009 $basename = implode("/", $parts) . '/images/upload/' . $row['id'] . '/'; if ( move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $basename.$filename)) { if (rename($basename.$filename, $basename . 'profilepicture.jpg') === true) echo 'File renamed.<br >' . "\n\r"; } Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899656 Share on other sites More sharing options...
dachshund Posted August 16, 2009 Author Share Posted August 16, 2009 still doesn't work. here's the whole thing if that helps. <?php include "../template/header.php"; //Сheck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000)) { //user $userid = $_SESSION['uid']; $query = "SELECT * FROM users WHERE id = '$userid'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); //Determine the path to which we want to save this file $parts = explode("/", dirname(__FILE__)); unSet($parts[(count($parts) - 1)]); rename($filename, "profilepicture.jpg"); $newname = implode("/", $parts) . '/images/upload/' . $row['id'] . '/' . $filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'].$newname))) { echo "It's done! The file has been saved as: ".$filename; } else { echo "Error: A problem occurred during file upload!" .' ' .$newname; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only .jpg images under 350Kb are accepted for upload"; } } else { echo "Error: No file uploaded"; } include "../template/footer.php"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899662 Share on other sites More sharing options...
dachshund Posted August 16, 2009 Author Share Posted August 16, 2009 thanks for trying btw! Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899665 Share on other sites More sharing options...
Andy-H Posted August 16, 2009 Share Posted August 16, 2009 <?php include "../template/header.php"; //Сheck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000)) { //user $userid = $_SESSION['uid']; $query = "SELECT * FROM users WHERE id = '$userid'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); //Determine the path to which we want to save this file $parts = explode("/", dirname(__FILE__)); unSet($parts[(count($parts) - 1)]); rename($filename, "profilepicture.jpg"); $newname = implode("/", $parts) . '/images/upload/' . $row['id'] . '/' . $filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $newname)) === true) { echo "It's done! The file has been saved as: ".$filename; } else { echo "Error: A problem occurred during file upload!" . ' ' . $newname; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only .jpg images under 350Kb are accepted for upload"; } } else { echo "Error: No file uploaded"; } include "../template/footer.php"; ?> Added comma instead of . in move_uploaded_file it takes 2 arguments. Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899666 Share on other sites More sharing options...
dachshund Posted August 16, 2009 Author Share Posted August 16, 2009 everything works fine except for the file rename. file uploads etc, but the name stays the same. Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899672 Share on other sites More sharing options...
dachshund Posted August 16, 2009 Author Share Posted August 16, 2009 it must just be the rename($filename, "profilepicture.jpg"); that isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899679 Share on other sites More sharing options...
dachshund Posted August 16, 2009 Author Share Posted August 16, 2009 i knew it'd be the simplest thing ever. i just changed rename($filename , "profilepicture.jpg"); to $filename = "profilepicture.jpg"; job done. Quote Link to comment https://forums.phpfreaks.com/topic/170559-solved-rename-file-upon-upload-last-question-of-the-evening/#findComment-899688 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.