Upload without reload the page

See all posts Reply

Upload without reload the page new!
by MDC888, 13 years, 6 months ago
Hi,

I have to upload an image inside a form, and I need to avoid reloading the page while uplaoding.

I really like this class (so useful...), so is there anyone who use it with ajax in order to do that?

Thanks!Reply
Re: Upload without reload the page new!
by Patrik, 13 years, 6 months ago
take a look at uploadify it does exactly what you need ( and it uses this upload.php file too)Reply
Re: Upload without reload the page new!
by Patrik, 13 years, 6 months ago
correction to prev. reply :

It doesnt use the class.upload.php however it still does what you needReply
Re: Upload without reload the page new!
by MDC888, 13 years, 6 months ago
Thanks!

I went through this class, and it seems nice, but I need to avoid flash, in order to be Iphone compliant :(

Then I found this:
http://www.dhtmlx.com/docs/products/dhtmlxVault/index.shtml
(Full ajax uploader)

Here is their PHP code:
$id  = $_GET['sessionId'];
$id = trim($id);

session_name($id);
session_start();
$inputName = $_GET['userfile'];
$fileName  = $_FILES[$inputName]['name'];
$tempLoc   = $_FILES[$inputName]['tmp_name'];

echo $_FILES[$inputName]['error'];
$target_path = 'uploads/';
$target_path = $target_path . basename($fileName);
if(move_uploaded_file($tempLoc,$target_path)) // here they are just moving the file from the tmp directory to the new


And here is what I did:
include('class.upload.php');

$id  = $_GET['sessionId'];
$id = trim($id);

session_name($id);
session_start();
$inputName = $_GET['userfile'];
$fileName  = $_FILES[$inputName]['name'];
$tempLoc   = $_FILES[$inputName]['tmp_name'];

$target_path = 'uploads/';

// ---------- IMAGE UPLOAD ----------
$handle = new Upload($tempLoc); // Processing the file from the tmp directory -> is that OK?

if ($handle->uploaded) {

    $handle->image_resize            = true;
    $handle->image_ratio_y           = true;
    $handle->image_x                 = 300;

    $handle->Process($target_path);
    // we check if everything went OK
    if ($handle->processed) {
        // everything was fine !
        $_SESSION['dhxvlt_state'] = -1;
    } else {
        // one error occured
        $_SESSION['dhxvlt_state'] = -3;
    }
}

But that's not working...

Any idea?

THANKS!!!Reply
Re: Upload without reload the page new!
by MDC888, 13 years, 6 months ago
I found the solution.
The upload class needs an array as parameter.

Here is a draft code if someone else needs it :

include('class.upload.php');

$id  = $_GET['sessionId'];
$id = trim($id);

session_name($id);
session_start();
$inputName = $_GET['userfile'];
$fileName  = $_FILES[$inputName]['name'];
$tempLoc   = $_FILES[$inputName]['tmp_name'];

// Prepare datas for upload class
$arr = array ( 
  'name' => $fileName,
  'type' => 'image/jpeg',
  'tmp_name' => $tempLoc,
  'error' => 0,
  'size' => '' ) ; // Don't know the size at this step
  
$target_path = 'uploads';
  
// ---------- IMAGE UPLOAD ----------

// we create an instance of the class, giving as argument the PHP object
// corresponding to the file field from the form
// All the uploads are accessible from the PHP object $_FILES
$handle = new Upload($arr);

// then we check if the file has been uploaded properly
// in its *temporary* location in the server (often, it is /tmp)
if ($handle->uploaded) {

  // yes, the file is on the server
  // below are some example settings which can be used if the uploaded file is an image.
  $handle->image_resize            = true;
  $handle->image_ratio_y           = true;
  $handle->image_x                 = 300;

  // now, we start the upload 'process'. That is, to copy the uploaded file
  // from its temporary location to the wanted location
  // It could be something like $handle->Process('/home/www/my_uploads/');
  $handle->Process($target_path);

  // we check if everything went OK
  if ($handle->processed) {
      // everything was fine !
      $_SESSION['dhxvlt_state'] = -1;
  } else {
      // one error occured
      $_SESSION['dhxvlt_state'] = -3;
  }

  // we delete the temporary files
  $handle-> Clean();
}
Reply
Re: Upload without reload the page new!
by colin, 13 years, 6 months ago
Thank you for your contributionReply