Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload File not recognised 1

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB

This has really got me stumped as it just doesn't work.
What confused me even more is that this is a copy of another file and I've simply amended the names.

Code:
<?php
session_start();
if (!isset($_SESSION['uid'])) {
	die ("You need to be logged in to view this page.<br /><a href='index.php'>Login</a>");
}
include_once ('/home/includes/dbc.php');
if ($_POST['Submit'] == 'Save') {
     $newdetails = $_POST['lowercontent'];
     $newimageALT = $_POST['lowercontentalt'];	
     //Retrieving Variables
     @$upload_Name = $_FILES['upload']['name'];
     @$upload_Size = $_FILES['upload']['size'];
     @$upload_Temp = $_FILES['upload']['tmp_name'];
     @$upload_Mime_Type = $_FILES['upload']['type'];
     if( $upload_Size == 0) {
          header("Location: home_edit.php?msg=File size is ZERO!");
          exit();
     }
     if( $upload_Mime_Type != "image/jpeg" AND $upload_Mime_Type != "image/gif" ) {
          unlink($upload_Temp);
          header("Location: home_edit.php?msg=Wrong type of image - only JPG or GIF");
          exit();
     }
//Where the file is upploaded to
$uploadFile = "../images/home".$upload_Name ;
if (!is_dir(dirname($uploadFile))) {
  @RecursiveMkdir(dirname($uploadFile));
} else {
  @chmod(dirname($uploadFile), 0777);
}
@move_uploaded_file( $upload_Temp , $uploadFile); 
chmod($uploadFile, 0755);

mysql_query("UPDATE home SET lowercontentimage='/images/home/$upload_Name',lowercontentalt='$newimageALT',lowercontent='$newdtails' WHERE id='1'");
     header("Location: main.php?msg=Content Updated");
}
?> 

<html>
<head>
<title>Blah!</title>
<link rel="stylesheet" type="text/css" href="./includes/newstyle.css" />

<script type="text/javascript" src="./jscripts/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
// General options
mode : "textareas",
//theme : "simple",
theme : "advanced",
//plugins
plugins : "emotions,phpimage,paste,directionality,fullscreen,style",
//plugins : "advimage,advlink,paste,directionality,fullscreen,imagemanager,filemanager",

// Theme options
theme_advanced_buttons1 : "bold,italic,forecolor,|,justifyleft,justifyright,justifyfull,|,charmap,|,sub,sup,|,hr,|,formatselect,fontselect,fontsizeselect,|,fullscreen",
theme_advanced_buttons2 : "cut,copy,paste,|,undo,redo,|,link,unlink,phpimage,|,emotions",
theme_advanced_buttons3 : "",
extended_valid_elements : "a[name|href|target|title|onclick]",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_layout_manager : "SimpleLayout"
});
</script>
</head>

<body>

<div id="container">

	<div id="banner"><h1>Admin Interface</h1></div>

	<div id="nav"><?php include './includes/nav.inc.php'; ?></div>
	
	<div id="content">

<?php
// retrieve the row from the database
$query = "SELECT * FROM home where id='1'";
$result = mysql_query( $query );
	
if( $result && $home = mysql_fetch_object( $result ) ) {
	// set our variables
	$lowercontentalt = $home -> lowercontentalt;
	$lowercontent = $home -> lowercontent;
}
//}

// print out the form
if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; }
?>

<form action="<?php echo $PHP_SELF; ?>" method="post">
	<br />
	<table width="80%" border="1" cellpadding="3" cellspacing="0" style="font-family:arial; font-size:13px;">
	
     <tr>
     <td valign="top">Lower Box Text</td>
     <td align="left" valign="top">
     <textarea name="lowercontent" rows="10" style="width:100%"><?php echo $lowercontent; ?></textarea>
     </td>
     </tr>
     
     <tr>
	<td>Left Image<br /><?php echo $lowercontentimage; ?></td>
	<td align="left" valign="top"><input type="file" name="upload" size="50">
	</td>
	</tr>

     <tr>
     <td>Image ALT text</td>
	<td align="left" valign="top">
	<input type="text" name="lowercontentalt" value="<?php echo $lowercontentalt; ?>" size="50" maxlength="255">
	</td>
	</tr>
   
   <tr>
   <td align="center" colspan="2">
   <input type="submit" name="Submit" value="Save">
   </td>
   </tr>

	</table>

   </form>         

	</div>

	<div id="footer">&nbsp;</div>

</div>

</body>
</html>

I've left the tinymce code in just for reference.
The error I get is that the file size is zero, but the file is definately not zero. I've tried other files but I continue to get this error.
If I comment out the filesize check, there is no name for the $upload_Name so I think somehow the file is not being recognised when the form is submitted.

I've got a feeling I'll get slightly embarrassed with the solution to this but at this moment in time, I don't really care 'cos this is causing my no end of frustration!
 

I hope I've put this in the right place, but when I add it to the page I get this

Array ( )

 
that means that nothing is getting uploaded.

try again but specify the enctype in the form tag (set the action as you need)
Code:
<form enctype="multipart/form-data" action="" method="post">

also, don't pass messages to the browser via the header command in the query string. that's not good practice.

as an alternative take one of two routes:

1. set a session variable; redirect to the error page; check the session variable and output message as needed
2. [much better] despatch the script to different functions and outputs depending on internal events. using this method you need NEVER to use header() for these purposes
 

Ah, I think that might be it!
I've just tried it and that was it!
Jeez, you will never know how much time I have spend on this upload form!

Thanks ...

And I need to look at the error message suggestion you mentioned. I used this method about 3 years ago on a site and like most things I suspected things had moved on, but I just found this easy to manage!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top