simcoweb Posted July 27, 2006 Share Posted July 27, 2006 I have a simple form (code below) just to test the file upload snippet. No matter what I do I get an error message while it's trying to write the temp name and transfer the image to the /uploads folder. Here's the code:[code]$image = "<img src='../uploads". $result['ID']. ".gif' border='0'/>";// we have to deal with the image upload firstif ( move_uploaded_file ($_FILES['image'] ['tmp_name'], "uploads/{$_FILES['uploadFile'] ['name']}") ) { print '<p> The file has been successfully uploaded </p>'; }else { switch ($_FILES['uploadFile'] ['error']) { case 1: print '<p> The file is bigger than this PHP installation allows</p>'; break; case 2: print '<p> The file is bigger than this form allows</p>'; break; case 3: print '<p> Only part of the file was uploaded</p>'; break; case 4: print '<p> No file was uploaded</p>'; break; } }[/code]Here's the form:[code]<html><title>This is a test PHP page</title><head></head><body><form action='test.php' method='POST' enctype="multipart/form-data"><input type="hidden" value="MAX_FILE_SIZE" value="100000" /><table width="600" border="0" align="center"><tr><td><font class="bodytext">Name:</td><td><input type="texbox" size="20" name="name"></td></tr><tr><td><font class="bodytext">Title:</td><td><input type="textbox" size="20" name="title"></td></tr><tr><td><font class="bodytext">Business Summary</td><td><textarea cols="20" rows="6" name="summary"></textarea></td></tr><tr><td colspan="2"><font class="bodytext">Select an image from your hard drive to upload for your profil</td></tr><tr><td colspan="2"><input type="file" size="20" name="image"/><tr><td colspan="2"><input type="submit" value="Send Profile"></form></td></tr></table></body></html>[/code]and here's the dang error message:[code]Warning: move_uploaded_file(uploads/): failed to open stream: Is a directory in /home/xxxxxx/public_html/test.php on line 11Warning: move_uploaded_file(): Unable to move '/tmp/phpdvlTi9' to '../uploads' in /home/xxxxxx/public_html/test.php on line 11[/code]It's driving me nuts. The permissions on the /uploads file is 777. The permissions on the /tmp folder which is located below the public area is 700. The server configuration will not allow it to be changed. That may be the problem but I don't know for sure until another set of eyes peer at the code to make sure it's not my bone headed code :) Quote Link to comment Share on other sites More sharing options...
sanfly Posted July 27, 2006 Share Posted July 27, 2006 While I have a close look at this, try adding a preceeding slash to the upload dir[quote]move_uploaded_file($_FILES['image']['tmp_name'], "[color=red]/[/color]uploads/{$_FILES['uploadFile']['name']}")[/quote] Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 27, 2006 Author Share Posted July 27, 2006 Hi, thanks for the response. I should've mentioned that I've tried every conceivable method of entering that directory:../uploads../uploads//uploads/uploadsuploads/All produce the exact same error except for the mention of the directory :( Quote Link to comment Share on other sites More sharing options...
sanfly Posted July 27, 2006 Share Posted July 27, 2006 What is the path of the upload directory relative to the directory where the script that executes the upload is located?Try this out, do you get the same error?:[code=php:0]$image = "<img src='../uploads". $result['ID']. ".gif' border='0'/>";$uploadDir = "/uploads/";// Check if directory existsif(!is_dir($uploadDir){ echo "ERROR: $uploadDir is not a valid directory<br><br>"; exit();}$srcFile = $_FILES['image']['tmp_name'];$newFile = $_FILES['uploadFile']['name'];if (move_uploaded_file($srcFile, "$uploadDir$newFile")){ print '<p> The file has been successfully uploaded </p>';}else{ switch ($_FILES['uploadFile'] ['error']){ case 1 : print '<p> The file is bigger than this PHP installation allows</p>'; break; case 2 : print '<p> The file is bigger than this form allows</p>'; break; case 3 : print '<p> Only part of the file was uploaded</p>'; break; case 4 : print '<p> No file was uploaded</p>'; break; }}[/code] Quote Link to comment Share on other sites More sharing options...
Drumminxx Posted July 27, 2006 Share Posted July 27, 2006 shouldn't this:move_uploaded_file ($_FILES['image'] ['tmp_name'], "uploads/{$_FILES['uploadFile'] ['name']}") be:move_uploaded_file ($_FILES['image'] ['tmp_name'], "uploads/{$_FILES['image] ['name']}") I do not see a file field named uploadFile on your form... Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 28, 2006 Author Share Posted July 28, 2006 sanfly, I inserted that code and got this error:[quote]Parse error: parse error, unexpected '{' in /home/xxxxx/public_html/test.php on line 38[/quote]which is this line:[code]38 if(!is_dir($uploadDir){39 echo "ERROR: $uploadDir is not a valid directory<br><br>";40 exit();41 }[/code]the 'test.php' script in relation to the uploads folder is as so:public_html/test.phppublic_html/uploads Quote Link to comment Share on other sites More sharing options...
Caesar Posted July 28, 2006 Share Posted July 28, 2006 [quote author=simcoweb link=topic=102110.msg404839#msg404839 date=1154045310]sanfly, I inserted that code and got this error:[quote]Parse error: parse error, unexpected '{' in /home/xxxxx/public_html/test.php on line 38[/quote][/quote][code]<?php38 if(!is_dir($uploadDir)){?>[/code] Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 28, 2006 Author Share Posted July 28, 2006 ahhh yes, the simplest things. Ok, added the additional right parentheses and now get this:[quote]ERROR: ../uploads/ is not a valid directory[/quote]Ok, before I made this reply I tried:/uploads/uploads/../uploadsuploads/all return the same error. Trust me, uploads IS a valid directory. And, the permissions are set at 777 so I know it's writeable. Quote Link to comment Share on other sites More sharing options...
Caesar Posted July 28, 2006 Share Posted July 28, 2006 What is the actual domain name?If you don't feel comfortable posting that, you can email it to me. Also, can you access the "/uploads" directory publically from your browser? Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 28, 2006 Author Share Posted July 28, 2006 no problem, it's http://www.templatedepot.com/uploads/I placed a dummy index page in there so it won't show the directory tree. Quote Link to comment Share on other sites More sharing options...
sanfly Posted July 28, 2006 Share Posted July 28, 2006 Sorry about the missing ')', I usually provide all my scripts with a disclaimer.Drumminxx is also correct about the naming issue, change this line: [code]$newFile = $_FILES['uploadFile']['name'];[/code]to this:[code]$newFile = $_FILES['image']['name'];[/code]I was also wrong about the preceeding slash, change this line: [code]$uploadDir = "/uploads/";[/code]to this:[code]$uploadDir = "uploads/";[/code]And just delete the whole directory check thing[code]// Check if directory existsif(!is_dir($uploadDir){echo "ERROR: $uploadDir is not a valid directory<br><br>";exit();}[/code]If it still doesnt work and you dont mind, please post the location of the file that is executing the script, so we can make sure the relative path is correct Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 28, 2006 Author Share Posted July 28, 2006 Yaaaaaaay! That did the trick! Ok, i'm taking notes. Thanks!One more question. HOW would I call this image file into say a table cell? I've tried using <img src="<? echo $image; ?>"> but that produced some other weird error about an 'unexpected ?'. That was before we fixed this code, though. Suggestions?Update: Just tried it again and get this:[quote]Parse error: parse error, unexpected '?' in /home/xxxxxx/public_html/test.php on line 61[/quote]That's this line: [quote]echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src="<?php echo $image; ?>"></td>";[/quote] Quote Link to comment Share on other sites More sharing options...
sanfly Posted July 28, 2006 Share Posted July 28, 2006 Your getting the error because the code is already within php tagsyou have this:[code]echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src="<?php echo $image; ?>"></td>";[/code]change it to this:[code]echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src=\"$image\"></td>";[/code] Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 28, 2006 Author Share Posted July 28, 2006 Ok, that makes perfect sense. I think I saw it done that way in some other post. Anyway, error message is gone but the image doesn't show. Just a broken link to it. If I pull up the pic's properties via my browser I get this location:[quote]http://www.templatedepot.com/%3Cimg%20src='uploads/.gif'%20border='0'/%3E[/quote]Something is not parsing correctly obviously. Quote Link to comment Share on other sites More sharing options...
sanfly Posted July 28, 2006 Share Posted July 28, 2006 What do you get if you just echo $imageeg[code=php:0]echo "$image";[/code] Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 28, 2006 Author Share Posted July 28, 2006 I'm parsing all this HTML under one echo statement which includes the $image tag utilizing the way you've specified it:[code]echo <<<HTML<head><title>Your Profile</title><link rel="stylesheet" type="text/css" href="tdepot.css"></head><body><table border='0' bgcolor='#FFFFFF' cellpadding='2' cellspacing='3' border='0' width='650'><tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src="$image"></td><td bgcolor='C0C0C0'><font class='bodytext'><i>$name</i></td></tr><tr><td bgcolor='#808080'><font class='bodytext'>$title</td></tr><tr><td bgcolor='#E9F2E4'><font class='bodytext'>$info</td></tr></table></body>HTML;[/code]After the page is parsed I get this as the image location:[code]http://www.templatedepot.com/%3Cimg%20src='uploads/.gif'%20border='0'/%3E[/code]The <'s are getting replaced somehow in the parsing of the code. Plus, I don't see any reference to the actual picture ID in the img src that's being pulled.You can view the form and try it yourself if you want: http://www.templatedepot.com/layout-test-form.htm. It's basically a test in file uploading and then parsing the data for the screen. :) Quote Link to comment Share on other sites More sharing options...
sanfly Posted July 29, 2006 Share Posted July 29, 2006 Okay,there are two reasons youre having a problemFirst and most obviously, when you define "$image", you have this[code=php:0]$image = "<img src='../uploads". $result['ID']. ".gif' border='0'/>";[/code]then when you call $image, you are putting it in the img src tag [i]again[/i][code]<img src=\"$image\">[/code]so, change the line:[code=php:0]echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src=\"$image\"></td>";[/code]to:[code=php:0]echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'>$image</td>";[/code]Secondly, the image name you are getting is[quote]'uploads/.gif[/quote]The image I tried uploading wasnt even a gif, and there is no file name.First, delete this line[code=php:0]$image = "<img src='../uploads". $result['ID']. ".gif' border='0'/>";[/code]Then BELOW where you have the code to upload the file, put this[code=php:0]$image = "<img src='$uploadDir$newFile' border='0'/>";[/code]and THEN put your HTML to show the imageLet me know how it goes.... Quote Link to comment Share on other sites More sharing options...
simcoweb Posted July 31, 2006 Author Share Posted July 31, 2006 Sorry, was gone for 3 days. Thanks for the response and further explanation. I see clearly now what you're talking about. I made the code changes and the image now shows up properly. Thanks! 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.