Jump to content

image upload


runnerjp

Recommended Posts

// file upload function
function SendResults( $errorNumber, $customMsg = '' )
{
echo '<script type="text/javascript">' ;
echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . $customMsg . '") ;' ;
echo '</script>' ;
exit ;
}

function GetRootPath() // from admin
{
$sRealRootPath = realpath( '../' ) ;
return $sRealRootPath;
}
function GetRootPathCMS()
{
$sRealRootPath = realpath( './' ) ;
return $sRealRootPath;
}

function GetURLPathFromAdmin() // from admin
{
$sRealPath = realpath( './' ) ;
$sRootRealPath = realpath( '../' ) ;
$sRealPathFromRoot = substr( $sRealPath, strlen( $sRootRealPath ), strlen( $sRealPath ) );

$sSelfPath = $_SERVER['PHP_SELF'] ;
$sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;

$sURLPath = substr( $sSelfPath, 0, strlen( $sSelfPath ) - strlen( $sRealPathFromRoot ) );

return $sURLPath;
}
function GetURLPath() // from root app
{
$sRealPath = realpath( './' ) ;
$sRootRealPath = realpath( './' ) ;
$sRealPathFromRoot = substr( $sRealPath, strlen( $sRootRealPath ), strlen( $sRealPath ) );

$sSelfPath = $_SERVER['PHP_SELF'] ;
$sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;

$sURLPath = substr( $sSelfPath, 0, strlen( $sSelfPath ) - strlen( $sRealPathFromRoot ) );

return $sURLPath;
}
function RemoveExtension( $fileName )
{
return substr( $fileName, 0, strrpos( $fileName, '.' ) ) ;
}

function RemoveFromStart( $sourceString, $charToRemove )
{
$sPattern = '|^' . $charToRemove . '+|' ;
return preg_replace( $sPattern, '', $sourceString ) ;
}

function RemoveFromEnd( $sourceString, $charToRemove )
{
$sPattern = '|' . $charToRemove . '+$|' ;
return preg_replace( $sPattern, '', $sourceString ) ;
}

function remove_directory($dir) {
  if ($handle = opendir("$dir")) {
   while (false !== ($item = readdir($handle))) {
     if ($item != "." && $item != "..") {
       if (is_dir("$dir/$item")) {
         remove_directory("$dir/$item");
       } else {
         unlink("$dir/$item");
       }
     }
   }
   closedir($handle);
   rmdir($dir);
  }
}

function upload_file($uploadFileName, $uploadDirectory, $admin_area = true) {
//upload new img title
if ($admin_area) $sServerDir = GetRootPath() . $uploadDirectory;
else $sServerDir = GetRootPathCMS() . $uploadDirectory;
// Get the posted file.
$oFile = $_FILES[$uploadFileName] ;
// Get the uploaded file name
$sFileName = $oFile['name'] ;
$sOriginalFileName = $sFileName ;
$sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
$sExtension = strtolower( $sExtension ) ;

// Initializes the counter used to rename the file, if another one with the same name already exists.
$iCounter = 0 ;

while ( true )
{
	// Compose the file path.
	$sFilePath = $sServerDir . '/' . $sFileName ;

	// If a file with that name already exists.
	if ( is_file( $sFilePath ) )
	{
		$iCounter++ ;
		$sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
	}
	else
	{
		move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;

		if ( is_file( $sFilePath ) )
		{
			$oldumask = umask(0) ;
			chmod( $sFilePath, 0777 ) ;
			umask( $oldumask ) ;
		}
		break ;
	}
}
return $sFileName;
}

function upload_file_cms($uploadFileName, $uploadDirectory) {
//upload new img title
$sServerDir = ROOT_PATH . $uploadDirectory;

// Get the posted file.
$oFile = $_FILES[$uploadFileName] ;
// Get the uploaded file name
$sFileName = $oFile['name'] ;
$sOriginalFileName = $sFileName ;
$sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
$sExtension = strtolower( $sExtension ) ;

// Initializes the counter used to rename the file, if another one with the same name already exists.
$iCounter = 0 ;

while ( true )
{
	// Compose the file path.
	$sFilePath = $sServerDir . '/' . $sFileName ;

	// If a file with that name already exists.
	if ( is_file( $sFilePath ) )
	{
		$iCounter++ ;
		$sFileName = RemoveExtension( $sOriginalFileName ) . '_' . $iCounter . '_.' . $sExtension ;
	}
	else
	{
		move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;

		if ( is_file( $sFilePath ) )
		{
			$oldumask = umask(0) ;
			chmod( $sFilePath, 0777 ) ;
			umask( $oldumask ) ;
		}
		break ;
	}
}
return $sFileName;
}

Link to comment
https://forums.phpfreaks.com/topic/260866-image-upload/#findComment-1337040
Share on other sites

Have you edited the code a all?

$sServerDir = ROOT_PATH . $uploadDirectory;

^ At first glance this seems to be the place where it saves the images.

 

Unless there is something I'm missing, then this would compile with a syntax error, because of ROOT_PATH.

Have you tried turning on error reporting?

 

Link to comment
https://forums.phpfreaks.com/topic/260866-image-upload/#findComment-1337057
Share on other sites

No its been moved from anouther server thats all

 

i have also added

<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

to each page with no errors

Link to comment
https://forums.phpfreaks.com/topic/260866-image-upload/#findComment-1337077
Share on other sites

No its been moved from anouther server thats all

 

i have also added

<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

to each page with no errors

 

you use 7 function calls, uhmm ok, i think u need only 1 ;)

Link to comment
https://forums.phpfreaks.com/topic/260866-image-upload/#findComment-1337193
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.