envexlabs Posted July 26, 2007 Share Posted July 26, 2007 Hey, I'm having a problem with my image uploader. Here is the file structure: root/inc/php/php_functions.php root/inc/ajax/add_product.php root/uploads php_functions.php upload function: function upload_image($upload_dir, $store_pic_id){ global $uploadfile; //grabs the file extension $file_ext = explode(".",$_FILES['userfile']['name']); //puts the array into a variable so that it can be inputed into the database as a name.ext instead of Array[] $name = $store_pic_id . '.' . $file_ext[1]; //sets the directory and name for the file $uploaddir = 'uploads/'. $upload_dir .'/'; $uploadfile = $uploaddir . basename($name); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {} else { echo "Picture could not be uploaded"; } //changes the permissions of the file chmod($upload_dir .'/' . $name . '', 0644); } add_product.php: <?php include'../php/config.php'; include'../php/php_functions.php'; echo '<p>'; $store_id = $_POST[store_id]; $pname = $_POST[pname]; $price = $_POST[price]; $desc = $_POST[desc]; $cat_id = $_POST[cat_id]; upload_image('product_images', $member_info[0]); $pic = $uploadfile; $add_product_query = mysql_query("INSERT INTO `products` (`product_id`, `store_id`, `name`, `price`, `sale_price`, `pic`, `description`, `display`, `on_sale`, `catagory`) VALUES (NULL, '$store_id', '$pname', '$price', '0', '$pic', '$desc', '1', '0', '$cat_id')") or die(mysql_error()); display_products($store_id, $cat_id); echo '</p>'; ?> I am getting this error when i try to upload it: Picture could not be uploaded Warning: chmod() [function.chmod]: No such file or directory in /mnt/gs02/herd02/20143/domains/werehavingasale.com/html/inc/php/php_functions.php on line 398 I know the function works because i use it on a file that is located in the root, but why isn't working when i use it in a file deep in the structure? Thanks, envex Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 26, 2007 Author Share Posted July 26, 2007 Ok, so i think i'm getting closer to figuring this out. $_FILES['userfile']['name'] is returning nothing. anyone know why? Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 26, 2007 Share Posted July 26, 2007 if $_FILES is coming up blank, there's a good chance you've forgotten to change the enctype attribute in the form tag. what is your form looking like at the moment? Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 26, 2007 Author Share Posted July 26, 2007 i added enctype="multipart/form-data" to the form, now i just have to work out some variable issues. will post soon! thanks, envex Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 26, 2007 Author Share Posted July 26, 2007 Hey, Alright, so it's still not working. i think $_FILES is still blank :S Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 26, 2007 Share Posted July 26, 2007 first, check what is and isn't blank after form submission. do: exit('<pre>'.print_r(get_defined_vars(), TRUE)); second, a few things to note about your function: 1. to get the extension, it's not reliable to use explode(). what if someone uploads a file named awful.exe.txt? it won't pick up the correct extension. you should use strrchr(). 2. no need to use basename() on the filename if you yourself are setting it. there won't be a path. 3. you can check for the existence of a directory and a file with is_file() and is_dir(); use this at your leisure to check for the existence of the target upload directory, check for the existence of the uploaded file after moving it, etc. this should aid with debugging. a silly question, but are you sure the file input is named 'userfile'? Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 26, 2007 Author Share Posted July 26, 2007 Hey, input file is named 'userfile', tripled checked that this s that i'm getting: [HTTP_POST_FILES] => Array ( ) [_FILES] => Array ( ) [_REQUEST] => Array ( [pname] => 123 [price] => 123 [desc] => 123 [cat_id] => 2 [userfile] => /Library/Application Support/Apple/iChat Icons/Planets/Neptune.gif [store_id] => 1 [bX] => a378nih37geu1&b=3&s=49 [phpSESSID] => 2655bb58e7be9d99100888e8eb2dd687 [amember_nr] => 9c5c925fcbe08ada07718365a0547d54 ) [store_id] => 1 [pname] => 123 [price] => 123 [desc] => 123 [cat_id] => 2 [uploadfile] => uploads/product_images/1. [pic] => uploads/product_images/1. [add_product_query] => 1 [product] => [owner] => ) So i'm not getting the extension, and it's not moving the uploaded file. Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 26, 2007 Share Posted July 26, 2007 i find it a wee bit odd that your server is setting the userfile variable, but not populating the $_FILES or $HTTP_POST_FILES array. what PHP version are you running, and what does your form file look like? Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 26, 2007 Author Share Posted July 26, 2007 We are slowly making progress var_dump(is_file($_FILES['userfile']['name'])) . "\n"; <-- returning false var_dump(is_dir($uploaddir)) . "\n"; <--returning true so the folder is recognized! here is my form code: <form action="javascript:get(document.getElementById(\'add_product\'));" name="add_product" enctype="multipart/form-data" id="add_product"> <p>Product Name:</p> <p><input type="text" id="pname" name="pname" class="reg" /></p> <p>Price:</p> <p><input type="text" id="price" name="price" class="reg" /></p> <p>Description:</p> <p><textarea id="desc" name="desc" cols="40" rows="3" class="edit_text" wrap="soft" onkeydown="textCounter(this.form.desc, this.form.remLen,100);" onkeyup="textCounter(this.form.desc, this.form.remLen,100); "></textarea></p> <input type="hidden" id="store_id" name="store_id" value="' . $store_id . '" /> <input type="hidden" id="cat_id" name="cat_id" value="' . $cat_id . '" /> <p><input name="remLen" type="text" id="remLen" value="100" size="3" maxlength="3" readonly class="remLen" /> characters remaining.</p> <p>Product Image:</p> <p><input name="userfile" id="userfile" type="file" /></p> <p><input type="image" src="images/add_product.gif" name="button" value="Submit"></p></form> I am on mediatemple running PHP Version 4.4.7 Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 26, 2007 Share Posted July 26, 2007 you need to use the POST method to submit files, not GET. switch the method (i can't tell exactly how you're submitting it via javascript, but i assume the get() function submits a GET request) and see if you start getting the file's submission. Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 26, 2007 Author Share Posted July 26, 2007 get is just the name of the AJAX function. It's still using POST to get all the form elements Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 26, 2007 Share Posted July 26, 2007 if PHP isn't actually receiving the relevant info, then it's most likely a javascript / form submission error, not a PHP error. from the variable info you posted earlier, it looks like PHP's not receiving the request info correctly, making me think it's the javascript. try submitting the form normally (ie. via a submit button and regular action attribute) and seeing if this actually sends the file. Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 26, 2007 Author Share Posted July 26, 2007 Hey, It looks like my AJAX script is screwing everything up. I'm getting this error when i execute the form: Warning: Form contains enctype=multipart/form-data, but does not contain method=post. Submitting normally with method=GET and no enctype instead. Source File: http://werehavingasale.com/store.php?store_id=1 Line: 0 It looks like my browser is reverting to GET even though the AJAX function is using POST. anyone know AJAX around here? Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 26, 2007 Share Posted July 26, 2007 try adding the method="post" to the form tag, and see if the AJAX function properly submits the form using the POST method. if that still fails, give the AJAX forum a visit and post the new problem (ie. show the form code and the AJAX function). Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 26, 2007 Author Share Posted July 26, 2007 Hey, adding method="post" didn't work, so i made a new thread in the AJAX forums. Thanks for all your help! envex 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.