Solved

error uploading image using

Posted on 2009-05-18
12
210 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:phillystyle123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24417352
@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.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24417361

<?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

0
 

Author Comment

by:phillystyle123
ID: 24417397
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);
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24417414
Not sure what that would do - try printing it out with var_dump() or something like that.
<?php // RAY_temp_philly.php
error_reporting(E_ALL);
 
//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);
 
var_dump(FILE_UPLOAD_LIMIT_BYTES);

Open in new window

0
 

Author Comment

by:phillystyle123
ID: 24417506
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
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24417535
"converting the files to ASCII" - my script does not do that - files upload correctly.
0
 

Author Comment

by:phillystyle123
ID: 24417561
any other ideas why that would happen? like i'm uploading bla.jpg and it turns into 232.bla?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24417569
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
0
 

Author Comment

by:phillystyle123
ID: 24417677
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');


0
 

Author Closing Comment

by:phillystyle123
ID: 31582799
thanks ray - i can use this for future file upload scripts.
0
 

Author Comment

by:phillystyle123
ID: 24420666
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

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24422987
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

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question