rvdb86 Posted November 5, 2009 Share Posted November 5, 2009 Hi hope someone can help me with this, I am trying to upload a file and save it in a specific folder. The file uploads correctly but it ends up in the wrong folder I want it to appear in the folder {root}/gallery/{gallery_id}/ but it ends up in {root}/gallery/ the script resides in {root}/admin/upload.php Heres my code: $upload_dir = "../gallery/".$_GET["apId"]; // Directory for file storing $web_upload_dir = "/tmp"; // Directory for file storing // FILEFRAME section of the script if (isset($_POST['fileframe'])) { //$fileNum = 1; $result = 'ERROR'; $result_msg = 'No FILE field found'; if (isset($_FILES['file'])) // file was send from browser { if ($_FILES['file']['error'] == UPLOAD_ERR_OK) // no error { // Random 25 chars+nums . . . $code = substr(md5(rand(0,9999)), 17, 25); // Get the file apart by ., so the 25 chars can be added before the extension $temp_arr = explode(".", $_FILES['file']['name']); // Default it $filename = $_FILES['file']['name']; // file name // Make sure the file isn't something like // image.something.whatever.hi.hello.what.s.up.png // Minus 2 because it would come out to // (file_name).(random_chars).(extension), // with this it comes out to (file_name)(random_chars).(extension) for ($x = 0; $x < count($temp_arr)-2; $x++) { // Add to it $filename .= $temp_arr[$x] . '.'; } // The new file name $new_file = $temp_arr[$x] . $code . '.' . $temp_arr[$x+1]; move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir.$new_file); // main action -- move uploaded file to $upload_dir $result = 'OK'; } } } any ideas? TIA Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/ Share on other sites More sharing options...
Bricktop Posted November 5, 2009 Share Posted November 5, 2009 Hi rvdb86, Does $_GET["apId"] contain data? Do you get the expected output if you echo this variable? Also, try adding a trailing slash to the $upload_dir variable, for example: $upload_dir = "../gallery/".$_GET["apId"]."/"; // Directory for file storing Hope this helps. Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/#findComment-951759 Share on other sites More sharing options...
rvdb86 Posted November 5, 2009 Author Share Posted November 5, 2009 Thanks for the suggestions Bricktop. I have tested the apId variable by echoing it on the page and it produces the correct value so it is not empty, I also tried to add the trailing slash but I am still facing the same problem. Could it be a problem with the server configuration? I actually have a similar script on a different server and it works perfectly Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/#findComment-951765 Share on other sites More sharing options...
Bricktop Posted November 5, 2009 Share Posted November 5, 2009 Hi rvdb86, Try amending your code to read: move_uploaded_file($_FILES['file']['tmp_name'], "$upload_dir/$new_file"); Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/#findComment-951767 Share on other sites More sharing options...
rvdb86 Posted November 5, 2009 Author Share Posted November 5, 2009 no changes :'( Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/#findComment-951774 Share on other sites More sharing options...
Bricktop Posted November 5, 2009 Share Posted November 5, 2009 Does echoing out the $upload_dir variable give the expected output? Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/#findComment-951781 Share on other sites More sharing options...
rvdb86 Posted November 5, 2009 Author Share Posted November 5, 2009 yes it does Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/#findComment-951786 Share on other sites More sharing options...
Bricktop Posted November 5, 2009 Share Posted November 5, 2009 OK, but I'm guessing the directory $_GET["apId"] refers to does not exist? i.e. you would like the dorectory to be created dynamically depending on what $_GET["apId"] contains. If so, move_uploaded_file() does not create directories. To confirm this, try manually creating a correct $_GET["apId"] folder, then running the above script. The file should write to the folder as the folder now exists. Report back what happens. Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/#findComment-951794 Share on other sites More sharing options...
rvdb86 Posted November 5, 2009 Author Share Posted November 5, 2009 Firstly Bricktop thanks for your help. I have found the problem but do not know how to overcome it. I am using ForceType in my .htaccess for friendly URLs but this seems to be messing with the location of the destination folder. Anyone know how I can continue using the ForceType and upload files through php? TIA! Link to comment https://forums.phpfreaks.com/topic/180412-problem-moving-uploaded-file/#findComment-951907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.