Solved

error uploading image using

Posted on 2009-05-18
12
197 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
  • 6
  • 6
12 Comments
 
LVL 108

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 108

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
 
LVL 108

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 108

Expert Comment

by:Ray Paseur
ID: 24417535
"converting the files to ASCII" - my script does not do that - files upload correctly.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
Finding (and replacing) text between two strings in Php 7 50
php ssh2_scp_send 1 46
Download a website to hdd 2 49
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now