Jump to content

image upload woes


envexlabs

Recommended Posts

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

Link to comment
Share on other sites

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'?

Link to comment
Share on other sites

Hey,

 

input file is named 'userfile', tripled checked that :P

 

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.

Link to comment
Share on other sites

We are slowly making progress :P

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.