Jump to content

Image upload problems


envexlabs

Recommended Posts

Hey,

 

I'll explain the whole spiderweb of files first:

 

When the add_product button is clicked, AJAX points to add_product.php, which executed the upload_file() function in php_functions.php

 

store.php <-- main page

add_product.php <-- page that ajax talks to when adding a product

 

Included files

 

inc/php_functions.php

inc/config.php

inc/ajax.js

 

 

 

php_functions.php (upload_file()):

 

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('uploads/'. $upload_dir .'/' . $name . '', 0644);
    
}

 

add_product.php:

include'inc/config.php';
include'inc/php_functions.php';

upload_image('store_images', '1');

$store_id = $_POST[store_id];
$pname = $_POST[pname];
$price = $_POST[price];
$desc = $_POST[desc];
    
$add_product_query = mysql_query("INSERT INTO `products` (`product_id`, `store_id`, `name`, `price`, `sale_price`, `pic`, `description`, `display`, `on_sale`) VALUES (NULL, '$store_id', '$pname', '$price', '0', 'store_tmb_test', '$desc', '1', '0')") or die(mysql_error());

 

when i execute the script, i get:

 

Array ( [pname] => [price] => [desc] => [userfile] => /Library/Application Support/Apple/iChat Icons/Tribal Masks/Makonde Mask.gif [store_id] => 1 )

 

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_functions.php on line 300

 

Now, the function works when i use a simple post to $_SERVER[php_self], but it wont work when i ajax the shit out of it :P

 

Any ideas?

 

 

 

Link to comment
Share on other sites

Here's a script that took me about a week to write. It's a method of uploading images through php into my webserver and adding the file names and locations to the sql database -

 

<?
$event = $_POST['event'];
$date = @$_POST['date'];
$flyer = @$_POST['uploaded'];
$user="xxxx";
$pass="xxxx";
$host = "xxxx";
$dbase="xxxx";
$table = "events";
mysql_connect($host,$user,$pass);
@mysql_select_db($dbase) or die("Unable to select database");
$query = "SELECT * FROM $table WHERE event ='$event'";
if ($result = mysql_query($query) or die(mysql_error()) > 0) {
  if (mysql_num_rows($result)) {
    echo "Error : This event is already in the database";
  } else
{
$filename = basename($_FILES['uploaded']['name']);
if((!empty($_FILES["uploaded"])) && ($_FILES['uploaded']['error'] == 0)) {
  $tag = "events";
  $ext = substr($filename, strrpos($filename, '.') + 1);
  $goback = "<a href=\"http://www.mysite.com/add_event.php\">back</a>";
  if (($ext == "jpg") || ($ext == "gif") && ($_FILES["uploaded"]["size"] < 3000000)) {
      $newname = dirname(events).'/upload/'.$filename;
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded']['tmp_name'],$newname))) {
           echo "You have successfully entered the following information into the database - <br> <br> Event : $event <br>Date : $date <br>Flyer : ";

$query = "INSERT INTO events VALUES ('','$event','$date','','$tag$newname')";
$result = mysql_query($query) or die(mysql_error());
mysql_close(); 
        } else {
           echo "Error: A problem occurred during file upload! Please go $goback and try again";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded"]["name"]." already exists. Please rename and go $goback and try again";
      }
  } else {
     echo "Error: Only jpg and gif files under 3MB are accepted for upload. <br> Go $goback and try again";
  }
} else {
echo "You have successfully entered the following information into the database - <br> <br> Event : $event <br>Date : $date";
} }}
?>

 

 

form page before -

 

<form enctype="multipart/form-data" action="/events/event_ul.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
  <tr>
    <td width="30%" align="left"><span class="time_main_text">Rave : </span></td>
    <td width="70%" align="left"><span class="style1"><input type="text" name="event" /><br>
  </tr>
  <tr>
    <td width="30%" align="left"><span class="time_main_text">Date : </span></td>
    <td width="70%" align="left"><span class="style1"><input type="text" name="date" /><br>
  </tr>
    <tr>
    <td width="30%" align="left"><span class="time_main_text">Flyer Image : </span></td>
    <td width="70%" align="left"><span class="style1"><input name="uploaded" type="file" /><br /></span></td>
  </tr>
  <br />
<tr>
    <td width ="30%" valign="top" align="center"><input name="UPDATE" type="submit" class="trk_list_main" id="UPDATE" value="Submit"/><input name="Clear" type="reset" class="trk_list_main" id="Clear" value="Reset"/></td>
    
</form>

 

 

Please don't ask me to descibe all this. I hope it's quite self explanatory. I have no idea about AJAX so I'll leave that one alone.

 

 

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.