I can't seem to consistently upload image files. My script enables me to upload some gifs of varying sizes no problemo, but with other gifs and jpegs they don't seem to appropriately upload. While the files will be moved to my uploads directory, they are inaccessible or corrupted or something. The page that calls the images up merely has the "red-ex/not accessible" icon where the image should be. The path is correct, and when I actually plug the path directly into a new browser window I get the same icon. Any idea how I can get my script to allow me to upload image files consistently?
Here's some of my script:
// upload form section
GIF Image Upload (optional):
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="userfile">
</div><br />
<input type="hidden" name="ItemType" value="Submit">
// IMAGE UPLOAD SECTION
// where file went on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
// original file name
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
// size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
// mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
// any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
if ($userfile)
{
if ($userfile_size == 0)
{
echo '<p class="backend" style="text-align:center;">Problem: uploaded file is zero length. <a href="insertnews.php">Go Back.</a></p>';
exit;
}
if ($userfile_error > 0)
{
echo '<p class="backend" style="text-align:center;">Problem: '.$userfile_error.'. <a href="insertnews.php">Go Back.</a></p>';
exit;
}
// PUT FILE WHERE YOU WANT IT
$upfile = 'd:/inetpub/wwwroot/Project/uploads/'.$userfile_name;
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo '<p class="backend" style="text-align:center;">Problem: Could not move file to destination directory. <a href="insertnews.php">Go Back.</a></p>';
exit;
}
}
$upfile = 'd:/inetpub/wwwroot/Project/uploads/'.$userfile_name;
echo '<p class="backend" style="text-align:center;">File '.$userfile_name. ' uploaded successfully</p>';
$HTTP_SESSION_VARS['userfile_name'] = $userfile_name;
// REFORMAT FILE CONTENTS
$fp = fopen($upfile, 'r');
$contents = fread($fp, filesize ($upfile));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);
}