Skip to Content Skip to Menu

SOLUTION FOUND: Cannot Upload Images

  • cambler
  • cambler
  • OFFLINE
  • Posts: 43
  • Thanks: 0
  • Karma: 0
16 years 3 months ago #56052 by cambler
SOLUTION FOUND: Cannot Upload Images was created by cambler
I, too, am having the same problem that others seem to be having uploading images.

Joomla 1.0.13 and the very latest component, downloaded today.

I've also traced it to the filesize() function on the $upload_pgitem_tmpname variable.

The file is NOT showing up in the temp directory. On my Windows server, that is in the c:/windows/temp directory (using backslashes, of course - regular slashes used here so they show up).

The size variable in the $_FILES array is correct, and the file I'm uploading is a very small 10K GIF for testing, so I'm not breaking any limit.

The file just doesn't show up in the temp directory, so, of course, the filesize() call returns 0.

The error entry in the $_FILES array is empty. Is there any other error condition I can be testing for?

Post edited by: cambler, at: 2008/02/05 20:56

Please Log in or Create an account to join the conversation.

  • cambler
  • cambler
  • OFFLINE
  • Posts: 43
  • Thanks: 0
  • Karma: 0
16 years 3 months ago #56105 by cambler
Replied by cambler on topic Re:Cannot Upload Images
I have found the solution - and I suspect anyone trying to use this component on a Windows (IIS) box might also have this problem.

In short, files uploaded DO NOT NECESSARILY SHOW UP IN THE TEMP DIRECTORY!

It would appear that in some cases, they're kept in memory.

Doing a filesize() on the temporary file will fail simply because it's not there.

But checking if (is_uploaded_file($file_array)) and then doing a move_uploaded_file() will succeed!

The solution, then, is to NOT RELY on the temporary upload file for checking. Do the move. If it succeeds, then do the rest of the work on the moved file.

I am coding this up myself, now, but suggest this as a bug fix.

Please Log in or Create an account to join the conversation.

  • cambler
  • cambler
  • OFFLINE
  • Posts: 43
  • Thanks: 0
  • Karma: 0
16 years 3 months ago #56116 by cambler
Replied by cambler on topic Re:SOLUTION FOUND: Cannot Upload Images
Here is the code for the re-worked function to resolve this problem. I release this back to the original author with my compliments.

Now, this said, I'm about to make some serious modifications (enhancements) to the codebase to allow for some things that my site needs, like multiple galleries, categories, etc. Would this be something that you (the original author) would like pushed back to you?

[code:1]
/**
* This function is called when a profile owner sumbits a new gallery item
* It should perform all validations and upload the file to the correct storage area and
* then call the _pgSave() function
* $this->_pgProcessNewItem($user->id,$pgautopublish,$pgtotalitems,$pgtotalsize);
*
* @access private
* @param int $id gallery item id
* @param object mosUser reflecting the user being displayed
*/
function _pgProcessNewItem($id, &$user)
{
global $my, $ueConfig, $mainframe, $mosConfig_lang;

$this->_getLanguageFile();

$PGItemAbsolutePath = $mainframe->GetCfg('absolute_path') . '/images/comprofiler/plug_profilegallery/';
$PGItemPath = '/images/comprofiler/plug_profilegallery/';

$html2return = "";

$userid = $user->id;

$tabparams = $this->_pgGetTabParameters($user);

$pgitemfilename = $this->_getPagingParamName("pgitemfilename"«»);
$pgitemtitle = $_POST[$this->_getPagingParamName("pgitemtitle"«»)];
$pgitemdescription = $_POST[$this->_getPagingParamName("pgitemdescription"«»)];
$pgitemorder = "999";

$upload_pgitem_name = $_FILES[$pgitemfilename];
$upload_pgitem_tmpname = $_FILES[$pgitemfilename];
$upload_pgitem_type = $_FILES[$pgitemfilename]["type"];
$upload_pgitem_size = $_FILES[$pgitemfilename]["size"];
$upload_pgitem_error = $_FILES[$pgitemfilename]["error"];

if (! is_uploaded_file($upload_pgitem_tmpname))
{
$html2return .= "<font color=red>" . _pg_NoFileUploaded . "</font><br />";

return $html2return;
}

$upload_pgitem_nameparts = explode(".", $upload_pgitem_name);
$upload_pgitem_ext = $upload_pgitem_nameparts[count($upload_pgitem_nameparts) - 1];
$upload_pgitem_baseparts = explode("." . $upload_pgitem_ext, $upload_pgitem_name);
$upload_pgitem_base = $upload_pgitem_baseparts[0];

$upload_pgitem_base = str_replace(" ", "_", $upload_pgitem_base);

$pgitemtype = $upload_pgitem_ext = strtolower($upload_pgitem_ext);

// First check size of uploaded item and stop right away if size has exceeded maximum allowable
//
if ($upload_pgitem_size > $tabparams["cbpguploadsize"] * 1024)
{
$html2return .= "<font color=red>" . _pg_MaxSizeExceeded . "</font><br />";

return $html2return;
}

// Generate random base name for upload
//
mt_srand((double) microtime() * 1000000);

$random_upload_pgitem_base = "pg_" . mt_rand();

// Check file extension type
//
$inimagelist = in_array($upload_pgitem_ext, explode(",", $tabparams["pgimagefiletypelist"]));
$infilelist = in_array($upload_pgitem_ext, explode(",", $tabparams["pgotherfiletypelist"]));

$consider_imgToolBox = 0;

switch ($tabparams["pgopmode"])
{
case 'IMAGEMODE' :

if (! $inimagelist)
{
$html2return .= "<font color=red>" . _pg_UnsupportedFileType . "</font><br />";
return $html2return;
}

$consider_imgToolBox = 1;

break;

case 'FILEMODE' :

if (! $infilelist)
{
$html2return .= "<font color=red>" . _pg_UnsupportedFileType . "</font><br />";
return $html2return;
}

$consider_imgToolBox = 0;

break;

case 'MIXEDMODE' :

if (! $infilelist && ! $inimagelist)
{
$html2return .= "<font color=red>" . _pg_UnsupportedFileType . "</font><br />";
return $html2return;
}

if (! $infilelist)
{
$consider_imgToolBox = 1;
}

break;

default :

$consider_imgToolBox = 0;

break;
}

// determine if user storage repository has been created (from some previous upload)
// if not create it now and give it proper permissions!
//
$PGItemAbsoluteUserPath = $PGItemAbsolutePath . $userid . "/";
$PGItemUserPath = $PGItemPath . $userid . "/";

if (! file_exists($PGItemAbsoluteUserPath))
{
if (mkdir($PGItemAbsolutePath . $userid, 0755))
{
if (copy($PGItemAbsolutePath . "index.html", $PGItemAbsoluteUserPath . "index.html"«»))
{
/* Succeed silently - no need to bother the user about this */

} else
{
print"<font color=red>" . _pg_IndexCreationFailure . "</font><br />";
}

$html2return .= "<font color=green>" . _pg_GalleryAreaCreated . "</font><br />";

} else
{
$html2return .= "<font color=red>" . _pg_GalleryAreaCreationFailed . "</font><br />";

return $html2return;
}
}

$final_uploaded_fullfilename = $PGItemAbsoluteUserPath . $random_upload_pgitem_base . "." . $upload_pgitem_ext;
$final_uploaded_tn_fullfilename = $PGItemAbsoluteUserPath . "tn" . $random_upload_pgitem_base . "." . $upload_pgitem_ext;
$final_uploaded_filename = $random_upload_pgitem_base . "." . $upload_pgitem_ext;

// Check to see if filename is unique and make it unique if not
//
$unique_suffix = 1;

$new_upload_pgitem_base = $random_upload_pgitem_base;

while (file_exists($final_uploaded_fullfilename))
{
$new_upload_pgitem_base = $random_upload_pgitem_base . "_" . $unique_suffix ++;
$final_uploaded_fullfilename = $PGItemAbsoluteUserPath . $new_upload_pgitem_base . "." . $upload_pgitem_ext;
$final_uploaded_tn_fullfilename = $PGItemAbsoluteUserPath . "tn" . $new_upload_pgitem_base . "." . $upload_pgitem_ext;
}

$final_uploaded_filename = $new_upload_pgitem_base . "." . $upload_pgitem_ext;

// Now move the uploaded file. We have to do this first, since on some IIS boxen we're not allowed (or able)
// to work on the temporary file!
//
if (! move_uploaded_file($upload_pgitem_tmpname, $final_uploaded_fullfilename))
{
$html2return .= "<font color=red>" . _pg_FileUploadFailed . "</font><br />";

return $html2return;
}

chmod($final_uploaded_fullfilename, 0755);

// Categorize uploaded item based on type attribute
//
list ($in_width, $in_height, $in_type, $in_attr) = getimagesize($final_uploaded_fullfilename);

$imgToolBox_needed_typecheck = 0;

if ($consider_imgToolBox)
{
switch ($in_type)
{
case 1 : /* GIF */
case 2 : /* JPG */
case 3 : /* PNG */

$imgToolBox_needed_typecheck = 1;

break;

default : /* All Other Types */

$imgToolBox_needed_typecheck = 0;

if ($pgitemtype == "gif" || $pgitemtype == "jpg" || $pgitemtype == "png"«»)
{
// trying to upload non image as image extension
//
$html2return .= "<font color=red>" . _pg_BadFile . "</font><br />";

unlink($final_uploaded_fullfilename);

return $html2return;
}

break;
}
}

if ($imgToolBox_needed_typecheck)
{
$imgToolBox = new imgToolBox();

$imgToolBox->_conversiontype = $ueConfig;
$imgToolBox->_IM_path = $ueConfig;
$imgToolBox->_NETPBM_path = $ueConfig;
$imgToolBox->_maxsize = $tabparams["cbpguploadsize"];
$imgToolBox->_maxwidth = $tabparams["pgmaxwidth"];
$imgToolBox->_maxheight = $tabparams["pgmaxheight"];
$imgToolBox->_thumbwidth = $tabparams["pgtnmaxwidth"];
$imgToolBox->_thumbheight = $tabparams["pgtnmaxheight"];
$imgToolBox->_debug = 0;

$imagePass = $_FILES[$this->_getPagingParamName("pgitemfilename"«»)];

// ImageToolBox expects a $_FILES array. That's fine, we'll fake one by replacing the temp file name
// with our moved file.
//
$imagePass = $final_uploaded_fullfilename;

$html2return .= "Image: " . $imagePass . "<br>FileName: $new_upload_pgitem_base<br>FilePath: $PGItemAbsoluteUserPath<br>";

// Note that we now tell processImage to use copy method 0 (no copy necessasry), as we've already done a move_uploaded_image() call
//
if (! ($newFileName = $imgToolBox->processImage($imagePass, $new_upload_pgitem_base, $PGItemAbsoluteUserPath, 0, 0, 0)))
{
$html2return .= "<font color=red>" . _pg_ImageToolBoxFailure . $imgToolBox->_errMSG . "</font><br />";

unlink($final_uploaded_fullfilename);

return $html2return;
}
}

$pgitemsize = filesize($final_uploaded_fullfilename);

if ($tabparams["cbpgtotalsize"] + $pgitemsize > $tabparams["cbpgtotalquotasize"] * 1024)
{
$html2return .= "<font color=red>" . _pg_QuotaExceeded . "</font><br />";

unlink($final_uploaded_fullfilename);

if (file_exists($final_uploaded_tn_fullfilename))
unlink($final_uploaded_tn_fullfilename);

return $html2return;
}

$new_cbpgtotalsize = $tabparams["cbpgtotalsize"] + $pgitemsize;

$new_cbpgtotalitems = $tabparams["cbpgtotalitems"] + 1;

// if we get here it means that we have validated the new entry and should finally save it to the database
//
$this->_pgSave($id, $pgitemorder, $pgitemtype, $final_uploaded_filename, $pgitemsize, $pgitemtitle, $pgitemdescription, $user, $new_cbpgtotalitems, $new_cbpgtotalsize, $tabparams["cbpgautopublish"], $tabparams["cbpgautoapprove"], $tabparams["pgmoderatornotification"]);

$successmessage = "";

if (! $imgToolBox_needed_typecheck)
{
$successmessage = "<font color=green>" . sprintf(_pg_FileUploadSucceeded, $final_uploaded_filename) . "</font><br />";
} else
{
$successmessage .= "<font color=green>" . sprintf(_pg_FileUploadAndTnSucceeded, $final_uploaded_filename, $successmessage) . "</font><br />";
}

if (! $tabparams["cbpgautoapprove"])
{
$successmessage .= "<br />" . "<font color=green>" . _pg_PendingApproval . "</font><br />";
}

return $successmessage;
}
[/code:1]

Please Log in or Create an account to join the conversation.

  • nant
  • nant
  • ONLINE
  • Posts: 25532
  • Thanks: 1834
  • Karma: 877
16 years 3 months ago #56135 by nant
Replied by nant on topic Re:SOLUTION FOUND: Cannot Upload Images
cool.

thanks for reporting, debugging and proposing solution!

any code suggestions - contributions are welcomed!

Post edited by: nant, at: 2008/02/06 07:33

Please Log in or Create an account to join the conversation.

  • sicinius
  • sicinius
  • OFFLINE
  • Posts: 4
  • Thanks: 0
  • Karma: 0
16 years 2 months ago #57681 by sicinius
Replied by sicinius on topic Re:SOLUTION FOUND: Cannot Upload Images
So we have the solution. How do we dummies implement it??

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum

Facebook Twitter LinkedIn