Jump to content
Larry Ullman's Book Forums

Undefined Variable In Add_Other_Products.php Script "ext"


Recommended Posts

After trying to add a product to the DB, I get an error saying there is an undefined variable (ext) in the add products script. Below is the lines where it is used:

 

$new_name .= ((substr($ext, 0, 1) != '.') ? ".{$ext}" : $ext);

 

Here is the whole IF statement:

 

if (!array_key_exists('image', $add_product_errors)) {
$new_name = sha1($file['name'] . uniqid('',true));
$new_name .= ((substr($ext, 0, 1) != '.') ? ".{$ext}" : $ext);
$dest =  "../products/$new_name";
if (move_uploaded_file($file['tmp_name'], $dest)) {
$_SESSION['image']['new_name'] = $new_name;
$_SESSION['image']['file_name'] = $file['name'];
echo '<h4>The file has been uploaded!</h4>';
} else {
trigger_error('The file could not be moved.');
unlink ($file['tmp_name']);
}
}
 
My question is should this be declared as empty? Basically declaring it like this:
 
if (!array_key_exists('image', $add_product_errors)) {
$ext = ' ';
$new_name = sha1($file['name'] . uniqid('',true));
$new_name .= ((substr($ext, 0, 1) != '.') ? ".{$ext}" : $ext);
$dest =  "../products/$new_name";
if (move_uploaded_file($file['tmp_name'], $dest)) {
$_SESSION['image']['new_name'] = $new_name;
$_SESSION['image']['file_name'] = $file['name'];
echo '<h4>The file has been uploaded!</h4>';
} else {
trigger_error('The file could not be moved.');
unlink ($file['tmp_name']);
}
}

 

 

Link to comment
Share on other sites

Found the error!

 

You defined the extension with $file_ext like so:

 

$file_ext = substr($file['name'], -4);
if ( !in_array($file_type, $allowed_mime) || !in_array($file_ext, $allowed_extensions) ) {
$add_product_errors['image'] = 'The uploaded file was not of the proper type.';
 
just changed to:
 
 
$ext = substr($file['name'], -4);
if ( !in_array($file_type, $allowed_mime) || !in_array($ext, $allowed_extensions) ) {
$add_product_errors['image'] = 'The uploaded file was not of the proper type.';
 
 
works like a charm!
  • Upvote 1
Link to comment
Share on other sites

  • 11 months later...

1) Hi there, I was experiencing the exact same problem and did the changes recommended above, however I am getting the following  

    message when I attempt to upload a product. The message is as follow:

========================================================

2) An error occurred in script '/home/content/82/11778682/html/admin/add_other_products.php' on line 87:
    move_uploaded_file(../products/276edc67b6955cf76faf19df99dc856ffb2ed121. ) [function.move-uploaded-file]: failed to open   

    stream: No such file or directory Array

========================================================

3) Can anyone point out where it is I am going wrong your help would be much appreciated to me and fellow forum members.

Link to comment
Share on other sites

So after doing a little further inquiries I was able to resolve the matter. I did not have a product folder in my admin sub-directory as a result the product image was not be store anywhere.

Also bear in mind if you are using a sub-directory make sure your

$dest =  "./products/$new_name";  that is line 85

is properly positioned within your file system otherwise you will be experiencing the same problem as I did in the above.

Link to comment
Share on other sites

So even though I was able to resolve that issue, I notice that the image is not getting stored in the data base and as a result it not showing up on the site either. So the price, description show but the image is not showing up. The image is in the product folder but not outputting on the site. Can anyone help with that issue?

Link to comment
Share on other sites

 Share

×
×
  • Create New...