Re: File Uploading - A possible attempt

Was that when u were looking at it on my computer or did you pinch the source? wink

the upload.cgi file is the helper app for working out the progress of the file.


Does anyone have anywhere I can host it that allows PHP CGI scripts to run?  My dumbass hoster wont let me use PHP CGI scripts and so I've had to resort to using my laptop sad

Re: File Uploading - A possible attempt

ah... it was when I pinched the source.  For some reason, when I clicked on the link Firefox started a download.  I thought your code was meant to be downloaded and not a working demo.  So I pinched it, and got only the .php source, of course.  A few modifications and it (sorta) worked.

Now when I click on it I get a demo.  Weird. 

Can I get a copy of the .cgi?  Not sure if it will work on my webhost's server though.

Re: File Uploading - A possible attempt

jsismail wrote:

For some reason, when I clicked on the link Firefox started a download.

yeah sorry about that, webserver was throwing a paddy.

the source code should be at http://203.97.10.134:8000/upload.cgi.src

54 (edited by jsismail 2006-06-19 5:12:54 PM)

Re: File Uploading - A possible attempt

BigBrownChunx wrote:

the source code should be at http://203.97.10.134:8000/upload.cgi.src

Sorry to be such a pain about this, but it seems the server isn't serving (throwing another paddy?).  Do you mind pasting the code directly in a reply?


ps.  If I can get it running on my webhost's server, we can put it there for all to see.

Re: File Uploading - A possible attempt

Yeah, after I pasted that link I had to leave work.  With my timezone the way it is, hosting it on my computer is the worst idea ever smile

Its a big dirty hack, but here's the code:

file_upload_test.php:

<?php
require ('xajax/xajax_core/xajax.inc.php');

$uid3 = time();

$xajax = new xajax();
//$xajax->debugOn();
$xajax->registerFunction('upload_progress');
$xajax->processRequest();

function upload_progress($uid, $progress_id, $form_id, $start=1)
{
    $objResponse = new xajaxResponse();
    $progress_file = get_progress_file($uid);
    if ($progress_file)
    {
        //$objResponse->alert($progress_file);
        list($progress, $total) = explode('/', file_get_contents($progress_file));
        $progress = (int) $progress;
        $total = (int) $total;
        if ($total===0)
            $percentage = -1;
        //$objResponse->alert($progress.' out of '.$total);
        else
            $percentage = (int)($progress/$total*100);
    } else {
        //assume file hasn't started since its not there
        $percentage = 0;
    }
    if ($percentage != -1)
    {
        $objResponse->assign($progress_id, 'innerHTML', '<div style="width: '.$percentage.'%; background-color: cyan; text-align: center; border: 1px solid blue;">'.$percentage.'%</div>'.$progress.'/'.$total.' bytes');
        $objResponse->assign($progress_id, 'style.backgroundColor', 'yellow');
    }
    //$done = !($start && $total===1);
    //if ($percentage == 100)
    {
        $objResponse->assign($form_id, 'style.display', 'block');
    } 
    //else
    {
        if ($percentage === 0 || $percentage === 100)
            sleep(1);
        else
            usleep(100000);
        //$objResponse->call('xajax_upload_progress',$uid, $progress_id, $form_id);
        $objResponse->script('xajax_upload_progress("'.$uid.'","'.$progress_id.'","'.$form_id.'", 0);');
    }
    return $objResponse;
}

function get_progress_file($uid)
{
    $progress_file = '/uploadp_'.$uid;
    $upload_tmp_dir = ini_get('upload_tmp_dir');
    if (file_exists($upload_tmp_dir.$progress_file))
        return $upload_tmp_dir.$progress_file;
    if (!$upload_tmp_dir && function_exists('sys_get_temp_dir'))
        $upload_tmp_dir = sys_get_temp_dir();
    if (file_exists($upload_tmp_dir.$progress_file))
        return $upload_tmp_dir.$progress_file;
    if (!$upload_tmp_dir)
        $upload_tmp_dir = ini_get('session.save_path');
    if (file_exists($upload_tmp_dir.$progress_file))
        return $upload_tmp_dir.$progress_file;
    if (!$upload_tmp_dir)
        $upload_tmp_dir = '/tmp';
    if (file_exists($upload_tmp_dir.$progress_file))
        return $upload_tmp_dir.$progress_file;
    return FALSE;
}

?><html><head><title>File Upload Test</title><? $xajax->printJavascript('xajax');?></head>
<body>
<?/*<script type="text/javascript"><!--
function convert_file_input(input_id)
{
    //grab the file input
    file_upload = xajax.$(input_id);
    //make sure we're really dealing with a file input
    if (file_upload.tagName.toLowerCase() != 'input' ||
        file_upload.type.toLowerCase() != 'file')
            return;
    
}
// --></script>
<input id="file_upload1" type="file" name="file_upload1" /><br/><br/>

<h2>Created with Javascript at run-time</h2>
<input id="file_upload2" type="file" name="file_upload2" /><br/><br/>
*/?>
<h2>Statically Created File Upload</h2>
<iframe id="file_upload_target3" name="file_upload_target3" style="height:100;width:400;border:1px dotted black;"></iframe>
<div id="file_upload_progress3" style="height: 50px; width: 300px; border: 1px solid red;"></div>
<form action="/cgi-bin/upload.cgi?UID=<?=$uid3;?>" method="post" enctype="multipart/form-data" target="file_upload_target3" id="file_upload_form3">
    <input id="file_upload3" type="file" name="file_upload3" />
    <input type="submit" value="Go" onclick="this.form.submit(); xajax_upload_progress(<?=$uid3;?>,'file_upload_progress3',this.id,1); return false;" /><br/>
    UID:<?=$uid3;?>
</form>
<input type="button" onclick="xajax.$('file_upload_target3').contentWindow.stop();return false;" value="Stop"/>
<br/><br/>
<i>Note:</i> None of the files that you upload are stored anywhere, they are read into memory to count the number of bytes sent so far, and at the end of the script are free'd.
<br/><br/>
<h2>Source Code:</h2>
<pre><?=highlight_file(__FILE__);?></pre><?=/*phpinfo();*/?>
</body></html>

upload.cgi:

#!/usr/bin/php
<?php //you need to change the above line to point to your php file
echo "Content-type: text/html\r\n\r\n";
flush();

echo 'Starting file upload...';
$starttime = time();
flush();
ob_end_flush();
flush();
ob_end_flush();
flush();

//$_GET isn't provided for us automatically
parse_str($_SERVER['QUERY_STRING'], $_GET);
$uid = (int) $_GET['UID'];
echo "\n".$uid."\n";
if (!$uid)
    $uid = mt_rand();

list($content_type, $mime_boundary) = explode(';',$_SERVER['CONTENT_TYPE']);
if ($content_type != 'multipart/form-data')
{
    //its not a file-upload-capable form
    //exit;
}

list($null, $mime_boundary) = explode('boundary=', $mime_boundary);


$total_size = (int) $_SERVER['CONTENT_LENGTH'];
$upload_tmp_dir = ini_get('upload_tmp_dir');
if (!$upload_tmp_dir && function_exists('sys_get_temp_dir'))
    $upload_tmp_dir = sys_get_temp_dir();
if (!$upload_tmp_dir)
    $upload_tmp_dir = ini_get('session.save_path');
if (!$upload_tmp_dir)
    $upload_tmp_dir = '/tmp';
$progress_file = $upload_tmp_dir.'/uploadp_'.$uid;
@unlink($progress_file);

//all the incoming file information comes through stdin
$stdin = fopen("php://stdin","r");
$cur_size = 0;
$chunk_size = 1000;  //read in 1k chunks
while($cur_size < $total_size)
{
    $chunk = fread($stdin, $chunk_size);
    $contents .= $chunk;
    $cur_size += strlen($chunk);
    
    //report how far we are through
    $progress_res = fopen($progress_file, 'w');
    flock($progress_res, LOCK_EX);
    fwrite($progress_res, $cur_size.'/'.$total_size);
    fclose($progress_res);
}
fclose($stdin);
$progress_res = fopen($progress_file, 'w');
flock($progress_res, LOCK_EX);
fwrite($progress_res, '1/1');
fclose($progress_res);


if (!$mime_boundary)
{
    //not a file upload
    //exit;
}

//split out all the data stored in the mime stuff
$fileparts = explode($mime_boundary, $contents);

//take of the first and last (blank) elements
array_pop($fileparts);
array_shift($fileparts);

//convert the mime info into the $_FILES global
foreach($fileparts as $filepart)
{
    list($header, $body) = explode("\r\n\r\n", $filepart);
    $header = trim($header);
    $header_lines = explode("\r\n", $header);
    foreach($header_lines as $line)
    {
        list($key, $value) = explode(': ',$line);
        if ($key == 'Content-Type')
        {
            $thisfile['type'] = $value;
        }
        else if ($key == 'Content-Disposition')
        {
            $disp_parts = explode('; ', $value);
            foreach($disp_parts as $disp_part)
            {
                list($disp_key, $disp_val) = explode('=',$disp_part);
                $disp_val = trim($disp_val, '"');
                if ($disp_key == 'name')
                {
                    $thisfilename = $disp_val;
                } else if ($disp_key == 'filename')
                {
                    $thisfile['name'] = $disp_val;
                }
            }
        }
    }
    $body = trim($body);
    $tmp_name = tempnam($upload_tmp_dir,'php');
    $thisfile['size'] = strlen($body);
    $thisfile['tmp_name'] = $tmp_name;
    //TODO: put in error codes
    $thisfile['error'] = 0;
    //TODO: this bit doesn't deal with names like filename[]
    $_FILES[$thisfilename] = $thisfile;
    
    //output the file to disk
    //$out = fopen($tmp_name, 'w');
    //fwrite($out, $body);
    //fclose($out);
}

/*
We now have a standard array of $_FILES
$_FILES['userfile']['name']
$_FILES['userfile']['type']
$_FILES['userfile']['size']
$_FILES['userfile']['tmp_name']
$_FILES['userfile']['error']
*/

echo 'done';
$endtime = time();

echo '
Took '.($endtime-$starttime).' seconds.';

//TODO make it call the calling script to pass on the $_FILES variable.
exit;

Re: File Uploading - A possible attempt

Thanks so much BBC.  I got sidetracked but as soon as I am able, I will try to run the code on my webhost's servers and if things work out, I 'll post a link so everyone can see the demo whenever they feel.

Re: File Uploading - A possible attempt

Could you give us an small walkthrough on how to use it wink

I never used cgi before.

Re: File Uploading - A possible attempt

The upload.cgi file needs to go into your cgi-bin folder (for me /var/www/localhost/cgi-bin/ -- i had to create this folder).
The first line of upload.cgi needs to point to your php executable (for me its /usr/bin/php)

The file_upload_test.php file just goes where ever.

Once you've got those two files uploaded, post a link so we can all try out the progress-bary goodness smile

59 (edited by statiic 2006-07-03 5:00:07 PM)

Re: File Uploading - A possible attempt

It seems that one of these arguments

onClick="this.form.submit(); xajax_upload_progress(<?= $uid3; ?>,'file_upload_progress3',this.id,1); return false;"

doesnt work with xajax 0.24. I think its the this.id . With what can I replace it.
Also an lot of your functions are based on xajax 0.5 wink
I had to change it back to 0.24 commands wink

And do you have any idea what kind of file directory I need for windows. I now use the full adress. Relative ones doesnt seem to work.

Or if anyone got this working with the 0.24 scripts. Then please post the source wink

Re: File Uploading - A possible attempt

statiic wrote:

Also an lot of your functions are based on xajax 0.5 wink
I had to change it back to 0.24 commands wink

where can I find xajax 0.5?!?!?!?!?!

wink

AN

Re: File Uploading - A possible attempt

Oh yeah, I forgot it was written using 0.5.  none of the line you posted is dependent on any version of xajax, statiic.

To get it to work in windows, you'll need to change the first line of upload.cgi to point to your php .exe file, you'll also need to change any reference of "/tmp" to "C:\Windows\Temp" in both the upload.cgi and file_upload_test.php files.

Maybe I'll ask Jared if he can host it?

Re: File Uploading - A possible attempt

OK I now got it working. You can have an look here:

http://www.heldermarketing.nl/test/file_upload.php

But it only loads into the memory. I hope someone can help me with an piece of code to save it to my harddrive.

Re: File Uploading - A possible attempt

Thanks for getting that to work statiic.  I'd be curious to know of any problems and solutions you had when installing the script under windows.

It only loads into memory on purpose: its a demo of what v0.5 is capable of (ok so it could work in 0.2.x too tongue)
I'll dig up the code to make it save to your hdd if you're desperate.

Re: File Uploading - A possible attempt

Ya kinda.

Cause I made my company website on xajax 0.2.4 and I dont want to convert it to xajax 0.5. It runs good on this version.

So I hope you can make some script that it writes to my harddrive then I can give it an nice look and add it to my website.

I will use xajax 0.5 for the CRM project I will start working on from the date xajax 0.5 is released wink

Re: File Uploading - A possible attempt

To make it work with windows and xajax 0.2.4. This is what you need to change.

->script  = ->addScript
->assign = ->addAssign

First line of the cgi script must direct to php.exe, not to the php folder. So something like C:\www\php\php.exe
The temp folder in both scripts must be an direct link too. Example : C:\www\root\temp\

The form in the upload.php script must be heading to the cgi-bin/upload.cgi

Thats it. If you changed everything correctly then it should work.

Now I hope somebody could tell me how to write the data from the memory to the harddrive. To make it completely work with xajax 0.2.4 wink

Good luck

Re: File Uploading - A possible attempt

BBC did you manage to make the file saving on the harddisk. Then I can finish my upload system for my clients. Cause I cant figure it out.

Greetz statiic

67 (edited by hifi 2006-08-02 9:00:05 PM)

Re: File Uploading - A possible attempt

Hi All,

I went through the complete thread but I am still confused about file uploading using xajax.

I am using the code from http://www.concepts.net.nz/xajax/file_u … xample.php but I am getting "Permission denied" error at line 67. when I debugged it is throwing the exception at

if (uploadIframe.contentDocument.body.innerHTML.indexOf('</xjx>') !== -1)

To be more specific

ok

uploadIframe.contentDocument

Not ok - Permission denied

uploadIframe.contentDocument.body

My OS is WindowsXP SP2 with "Disable_Local_Machine_Navigate" set to 0.

Any help would be appreciated.

Thanks in anticipation.
PS: BBC, Could you upload the example with the progress bar somewhere as http://www.heldermarketing.nl/test/file_upload.php is not working?

Re: File Uploading - A possible attempt

Those scripts aint working proparly. Use the last script I posted for windows and the last script BBC posted for linux.
Good luck

Re: File Uploading - A possible attempt

Thanks for the reply, but I am not able to find the link of your last script.

Regards

Re: File Uploading - A possible attempt

I havn't found the time to finish the script, statiic.  I'll get it finished before we release v0.5 though smile

hifi, the link you post there saying permission denied was removed from this topic as I dont work for that company any more.  i guess i need to talk to Jared about asking if i can put a cgi script on the xajax site instead.

71 (edited by hifi 2006-08-03 9:27:39 AM)

Re: File Uploading - A possible attempt

BBC,

I am getting the error "Permission denied" on my local machine also.

Please help me with the final script which I should use before v0.5

Regards

Re: File Uploading - A possible attempt

Do you have windows or linux running on your server?

Re: File Uploading - A possible attempt

Hi Statiic,

I have windows xp sp2.
server - Apache
xajax 0.2.4.

Regards

Re: File Uploading - A possible attempt

Ok Then use the script which is posted on this page by BBC.

I have posted also on this page an small walkthrough on how to make it compatible with windows and xajax 0.2.4. Follow that one and it should work just fine;)

Re: File Uploading - A possible attempt

Hi All,

My work is almost stuck becuase of this file uploading using xajax. Could anyone please post a working example? I would really appreciate.
Statiic, I tried the script posted my BBC on this page but I am puzzled. Could you post your working code?
I have both the requirements to save images in the database and on the file system. How can I do that?

Thanks in anticipation.
hifi