PDA

View Full Version : PHP code problem


Mickie D
05-22-2015, 04:29 PM
Hi all,

I have an issue with this code. What seems to happen is when it creates the file from the chunked upload it stores it in

/temp/

It should join all the parts then store it here

/temp/USERNAME/



$temp_dir = '/temp/'.$vbulletin->userinfo['username'].'/'.$_POST['resumableIdentifier'];
$dest_file = $temp_dir.'/'.$_POST['resumableFilename'].'.part'.$_POST['resumableChunkNumber'];

// create the temporary directory
if (!is_dir($temp_dir)) {
mkdir($temp_dir, 0777, true);
}





this is part of the function that works with it.


if ($total_files * $chunkSize >= ($totalSize - $chunkSize + 1)) {

// create the final destination file
if (($fp = fopen('/temp/'.$vbulletin->userinfo['username'].'/'.$fileName, 'w')) !== false) {

fclose($fp);
} else {
_log('cannot create the destination file');
return false;
}

// rename the temporary directory (to avoid access from other
// concurrent chunks uploads) and than delete it
if (rename($temp_dir, $temp_dir.'_UNUSED')) {
rrmdir($temp_dir.'_UNUSED');
} else {
rrmdir($temp_dir);
}
}

kh99
05-22-2015, 04:49 PM
Where is that code going (if it's a plugin, which hook)? You might need a
global $vbulletin;

statement in your code.

Mickie D
05-22-2015, 04:52 PM
Hi again KH99

This is in a custom php page, it calls global at the beginning...

I even wrote out an template register variable that calls $temp_dir

and it outputs

/temp/Mick/

But I am not sure why its writing the file to /temp/ and NOT /temp/Mick



// ######################### REQUIRE BACK-END ############################
// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line
// chdir ('/path/to/your/forums');
require_once('./global.php');


Thank you.

kh99
05-22-2015, 04:59 PM
Oh, so the part that's missing is $fileName. I don't see that being set anywhere in the code you posted.

Mickie D
05-22-2015, 05:01 PM
ahh sorry,

I missed the function

the function

function createFileFromChunks($temp_dir, $file, $chunkSize)

// check that all the parts are present
// the size of the last part is between chunkSize and 2*$chunkSize
if ($total_files * $chunkSize >= ($totalSize - $chunkSize + 1)) {

// create the final destination file
if (($fp = fopen('/temp/'.$vbulletin->userinfo['username'].'/'.$fileName, 'w')) !== false) {
for ($i=1; $i<=$total_files;)
}
fclose($fp);
} else {
_log('cannot create the destination file');
return false;
}

// rename the temporary directory (to avoid access from other
// concurrent chunks uploads) and than delete it
if (rename($temp_dir, $temp_dir.'_UNUSED')) {
rrmdir($temp_dir.'_UNUSED');
} else {
rrmdir($temp_dir);
}
}

}




the code

if (!empty($_FILES)) foreach ($_FILES as $file) {

// check the error status
if ($file['error'] != 0) {
_log('error '.$file['error'].' in file '.$_POST['resumableFilename']);
continue;
}

// init the destination file (format <filename.ext>.part<#chunk>
// the file is stored in a temporary directory
$temp_dir = '/temp/'.$vbulletin->userinfo['username'].'/'.$_POST['resumableIdentifier'];
$dest_file = $temp_dir.'/'.$_POST['resumableFilename'].'.part'.$_POST['resumableChunkNumber':


}


}

Dave
05-22-2015, 06:29 PM
It's because the $vbulletin variable can not be reached inside the function you use.
You have to use "global $vbulletin;" inside of the function or pass the $vbulletin variable as a 5th argument to the function.

Mickie D
05-22-2015, 07:58 PM
It's because the $vbulletin variable can not be reached inside the function you use.
You have to use "global $vbulletin;" inside of the function or pass the $vbulletin variable as a 5th argument to the function.

Excellent Dave... Thank you very much. That worked perfect

Is that the same with all functions? If I want to use vbbulletin variables

Cheers

cellarius
05-23-2015, 06:14 AM
Yes, that's always the case, not only in vB. Global variables are never available inside functions in PHP, you always need to pull them in like that.

Mickie D
06-01-2015, 10:02 AM
Yes, that's always the case, not only in vB. Global variables are never available inside functions in PHP, you always need to pull them in like that.

Thanks been doing allot of reading - bought a few books and found out quite a bit already!

Thank you all for the support

Mick