Link to home
Start Free TrialLog in
Avatar of phillystyle123
phillystyle123Flag for United States of America

asked on

error uploading image using

Error uploading image: 0

im using the following to upload images to the server and place the imge name in the db. i'm guessing this is a problem with my configuration file. the following is the form element - but it seems like no matter where i try to do this on my cms (which i did not create so i'm assuming this has worked on other servers) i get this error:

Error uploading image: 0 - so this is in my database fleld.

actually - somehow in the midst of all of this one file was uplaoded to the proper directory but it was changed from a jpg to this: h-7354.haines  

form element:

<input type="hidden" name="currentimage" value="<?= $imagefile; ?>" />
                        <? } ?>
                        <tr>
                              <td>Photo: <em><small><? if (strlen($imagefile)) echo "Upload only to overwrite exisiting photograph"; ?></small></em><br />
                              <input type="file" name="imagefile" size="25" maxlength="255" tabindex="<?= $ak->tabindex($key); ?>" />
config file:
 
<?
$is_dev = ($_SERVER['HTTP_HOST'] == 'localhost') ? 1 : 0; //Used for changing the database connection on-the-fly
define('SITE_NAME_SIMPLE','www.example.net/~bladir/');
define('SITE_NAME_FULL','http://www.example.net/~bladir/');
define('ROOT_URL_TO_ADMIN', ($is_dev) ? '' : SITE_NAME_FULL . '/admin/');
define('NAV_FORMAT', 'inline');
define('HTML_EDITOR', ($is_dev) ? 1 : 1);
define('DB', ($is_dev) ? 'twt' : 'bladir_twt');
define('USERNAME', ($is_dev) ? 'root' : 'bladir_Dtaylor');
define('PASSWORD', ($is_dev) ? '' : 'dJnBVQY7db');
define('SERVER', ($is_dev) ? 'localhost' : '1.111.1111');
 
//Determine operating system and set up appropriate folder separator
define('DOCROOT', stristr($_SERVER['SystemRoot'], 'Win') ? "C:\\wamp\\www\\bladir" : '/home/bladir/public_html');
define('FS', stristr($_SERVER['SystemRoot'], 'Win') ? "\\" : "/");
define('MODULES_F', DOCROOT . FS . 'modules');
define('IMGROOT', DOCROOT . FS . 'uploads' . FS);
 
//Define all possible subfolders for uploads
define('BIO_F', IMGROOT . 'biographies');
define('TIMELINE_F', IMGROOT . 'timeline');
define('EVENTS_F', IMGROOT . 'events');
define('GALLERY_F', IMGROOT . 'gallery');
define('NEWS_F', IMGROOT . 'news');
define('PAGES_F', IMGROOT . 'pages');
define('PORTFOLIO_F', IMGROOT . 'portfolio');
define('FRONTPAGE_F', IMGROOT . 'frontpage images');
define('QUOTE_F', IMGROOT . 'quotes');
define('SEO_HTACCESS', DOCROOT . FS . '.htaccess');
define('CLIENT_F', DOCROOT . FS . 'clientfolders');
define('MODULE_F', DOCROOT . FS . 'zip_repository');
define('EMAILTMP_F', CLIENT_F . FS . 'emailattachments');
 
//File upload limit	
define('FILE_UPLOAD_LIMIT', substr(ini_get('upload_max_filesize'), 0, strlen(ini_get('upload_max_filesize')) - 1));
define('FILE_UPLOAD_LIMIT_BYTES', (FILE_UPLOAD_LIMIT * 1000) * 1024);
 
//Used for blog postings; ReCAPTCHA
define('PUBLIC_KEY', 'xxx');
define('PRIVATE_KEY', 'xxx');
 
//Do database connection here. Prevents re-connection every db call
define('SERVER_CONNECT', mysql_connect(SERVER, USERNAME, PASSWORD, true));
 
//Make sure register globals is off
if (ini_get('register_globals')) {
	$superglobals = array($_SERVER, $_ENV, $_FILES, $_COOKIE, $_POST, $_GET);
	if (isset($_SESSION)) {
		array_unshift($superglobals, $_SESSION);
	}
	foreach ($superglobals as $superglobal) {
		foreach ($superglobal as $global => $value) {
			unset($GLOBALS[$global]);
		}
	}
	ini_set('register_globals', false);
}
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

@phillystyle123: I'll post an example of how to do the upload.  With a little creativity, you can get the file names into the data base.

<?php // RAY_upload_example.php
error_reporting(E_ALL);
 
// MANUAL REFERENCE PAGES
// http://docs.php.net/manual/en/features.file-upload.php
// http://docs.php.net/manual/en/features.file-upload.common-pitfalls.php
// http://docs.php.net/manual/en/function.move-uploaded-file.php
// http://docs.php.net/manual/en/function.getimagesize.php
 
// ESTABLISH THE NAME OF THE 'uploads' DIRECTORY
$uploads = 'uploads';
 
// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT
$max_file_size = '8192000';  // EIGHT MEGABYTE LIMIT ON UPLOADS
 
// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT
$file_exts = array('jpg', 'gif', 'png', 'txt');
 
// ESTABLISH THE NUMBER OF FILES WE CAN UPLOAD
$nf = 3;
 
 
 
// THIS IS A LIST OF THE POSSIBLE ERRORS THAT CAN BE REPORTED in $_FILES[]["error"]
$errors	= array(
	0 => "Success!",
	1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
	2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
	3 => "The uploaded file was only partially uploaded",
	4 => "No file was uploaded",
	6 => "Missing a temporary folder",
	7 => "Cannot write file to disk"
);
 
 
 
// IF THERE IS NOTHING IN $_POST, PUT UP THE FORM FOR INPUT
if (empty($_POST))
{
	?>
	<h2>Upload <?=$nf?> file(s)</h2>
 
	<!--
		SOME THINGS TO NOTE ABOUT THIS FORM...
		NOTE THE CHOICE OF ENCTYPE IN THE HTML FORM STATEMENT
		MAX_FILE_SIZE MUST PRECEDE THE FILE INPUT FIELD
		INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN $_FILES ARRAY
	-->
 
	<form name="UploadForm" enctype="multipart/form-data" action="<?=$_SERVER["REQUEST_URI"]?>" method="POST">
	<input type="hidden" name="p" value="1" />
	<input type="hidden" name="MAX_FILE_SIZE" value="<?=$max_file_size?>" />
	<p>
	Find the file(s) you want to upload and click the "Upload" button below.
	</p>
 
	<?php for ($n = 0; $n < $nf; $n++)
		{
			echo "<input name=\"userfile$n\" type=\"file\" size=\"80\" /><br/>\n";
		}
	?>
	<br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <b>overwrite</b> existing files.
	<input type="submit" name="_submit" value="Upload" />
	</form>
	<?php
	die();
}
 
 
 
else // WE HAVE GOT SOMETHING IN $_POST
{
 
// THERE IS POST DATA - PROCESS IT
	echo "<h2>Results: File Upload</h2>\n";
 
// ACTIVATE THIS TO SEE WHAT IS COMING THROUGH
//	echo "<pre>"; var_dump($_FILES); var_dump($_POST); echo "</pre>\n";
 
// ITERATE OVER THE CONTENTS OF $_FILES
	foreach ($_FILES as $my_uploaded_file)
	{
 
// SKIP OVER EMPTY SPOTS - NOTHING UPLOADED
		$error_code	= $my_uploaded_file["error"];
		if ($error_code == 4) continue;
 
// SYNTHESIZE THE NEW FILE NAME
		$f_type	= trim(strtolower(end    (explode( '.', basename($my_uploaded_file['name'] )))));
		$f_name	= trim(strtolower(current(explode( '.', basename($my_uploaded_file['name'] )))));
		$my_new_file = getcwd() . '/' . $uploads . '/' . $f_name .'.'. $f_type;
		$my_file     = $uploads . '/' . $f_name .'.'. $f_type;
 
// OPTIONAL TEST FOR ALLOWABLE EXTENSIONS
		if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");
 
// IF THERE ARE ERRORS
		if ($error_code != 0)
		{
			$error_message = $errors[$error_code];
			die("Sorry, Upload Error Code: $error_code: $error_message");
		}
 
// GET THE FILE SIZE
		$file_size	= number_format($my_uploaded_file["size"]);
 
// MOVE THE FILE INTO THE DIRECTORY
// IF THE FILE IS NEW
		if (!file_exists($my_new_file))
		{
			if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
			{
				$upload_success = 1;
			}
			else
			{
				$upload_success = -1;
			}
 
// IF THE FILE ALREADY EXISTS
		}
		else
		{
			echo "<br/><b><i>$my_file</i></b> already exists.\n";
 
// SHOULD WE OVERWRITE THE FILE? IF NOT
			if (empty($_POST["overwrite"]))
			{
				$upload_success = 0;
 
// IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
			}
			else
			{
				$now	= date('Y-m-d');
				$my_bak = $my_new_file . '.' . $now . '.bak';
				if (!copy($my_new_file, $my_bak))
				{
					echo "<br/><b>Attempted Backup Failed!</b>\n";
				}
				if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
				{
					$upload_success = 2;
				}
				else
				{
					$upload_success = -1;
				}
			}
		}
 
// REPORT OUR SUCCESS OR FAILURE
		if ($upload_success == 2) { echo "<br/>It has been overwritten.\n"; }
		if ($upload_success == 1) { echo "<br/><b><i>$my_file</i></b> has been saved.\n"; }
		if ($upload_success == 0) { echo "<br/><b>It was NOT overwritten.</b>\n"; }
		if ($upload_success < 0)  { echo "<br/><b>ERROR <i>$my_file</i> NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</b>\n"; }
		if ($upload_success > 0)
		{
			echo "$file_size bytes uploaded.\n";
			if (!chmod ($my_new_file, 0755))
			{
				echo "<br/>chmod(0755) FAILED: fileperms() = ";
				echo substr(sprintf('%o', fileperms($my_new_file)), -4);
			}
			echo "<br/><a href=\"$my_file\">See the file $my_file</a>\n";
		}
// END ITERATOR
	}
}
?>

Open in new window

Avatar of phillystyle123

ASKER

thanks ray - i'm looking at it now - how does anyone remain sane in this busienss.

please take a look at this block - i have a feeling i'm not setting this properly - please advise - and thanks:

//File upload limit      
define('FILE_UPLOAD_LIMIT', substr(ini_get('upload_max_filesize'), 0, strlen(ini_get('upload_max_filesize')) - 1));
define('FILE_UPLOAD_LIMIT_BYTES', (FILE_UPLOAD_LIMIT * 1000) * 1024);
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well- i didn't do that but i've added some of your numbers to the code and it seems to be uploading but it almost seems like it's converting the files to ASCII
"converting the files to ASCII" - my script does not do that - files upload correctly.
any other ideas why that would happen? like i'm uploading bla.jpg and it turns into 232.bla?
No idea at all - that is a logic error in the upload script, and you have not posted the upload script.  But since we know for sure that the upload script is defective, why not start with my example and recode on top of that platform?  It will at least give you a working foundation to start from!

Best regards, ~Ray
i'm not seeing an upload script anywyhere -

this is on the same page as my upload form:

$filename = fu::uploadImage($_FILES['imagefile'], TIMELINE_F);

this is why i thought there was a connection wih this line from my config file:
define('TIMELINE_F', IMGROOT . 'timeline');


thanks ray - i can use this for future file upload scripts.
just realized what the actual problem was in this instance - the filenames of the images i'm trying to upload:

i.e. h.bla.jpg

i think the 1st period in the filename is throwing my image application off

You may be able to use basename() to help with that issue.

Thanks for the points, and good luck with it, ~Ray
<?php // RAY_temp_basename.php
error_reporting(E_ALL);
$thing = basename("../abc/h.bla.jpg");
var_dump($thing); // PRINTS h.bla.jpg
?>

Open in new window