Jump to content
Larry Ullman's Book Forums

Image Upload Code Not Working In Larger Script


Recommended Posts

Hello again! I am designing a small community for people online, and I wanted an image upload function for avatars. Naturally I referenced Larry's book and found the image upload code to help me along on my project. I first used the following code in its own right, and it uploaded the file directly to the uploads file indicated on my server. However, when I pasted the code into the script (having changed the form action to the new script's name) the image upload function stops working meaning it does not upload any file to the uploads folder. I don't know why this would happen because I don't touch the code for the upload aside from changing the form action from action="image.php" (the image upload script alone) to action="user.php" (a user edit page).

 

Could anybody point out what I might be doing wrong here? I'm a noob so I apologise if the answer is obvious. Many thanks for all of your help!

 

Code for the image upload script by itself (which works and uploads the file to the server) is:

 

	<?php 

	// Check that a file was uploaded...

	if (is_uploaded_file($_FILES['image']['tmp_name'])) {

		// Create a temporary file name...

		$temp = 'uploads/' . md5($_FILES['image']['name']);

		// Move the file over...

		if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) {

			$i = $_FILES['image']['name'];

		} else {

		}

	} else {

	}

	echo '



	<form enctype="multipart/form-data" action="user.php" method="post">
	<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
	<p>Upload your photograph!   <input type="file" name="image" />
	<input type="submit" name="submit" value="Upload"/>
	<input type="hidden" name="submitted" value="TRUE"/>

	'

	?>

 

The code which does not and is part of a larger script is the following:

 

<?php // user.php


// Include the essential files required for the profile, whether it is displayed or not...
include ('includes/header.html');

// Connect to the database.
require_once('mysqli_connect.php');

// Which user are we looking at?

if (isset($_GET['user_id']) && is_numeric($_GET['user_id'])) { // Does the ID exist in the $_GET array?

$id = (int) $_GET['user_id'];

if ($id > 0) { // If the ID is greater than 1, then run the query.

	$q = "SELECT * FROM users WHERE user_id='$id' LIMIT 1";
	$r = mysqli_query($dbc, $q);

}


	if (mysqli_num_rows($r) == 1) { // If the query brings back the row, then store it in the row array.

		$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

		// Make sure the first name and last name are uppercase when displayed...
		$fn = $row['first_name']; $fn = ucfirst($fn);
		$ln = $row['last_name']; $ln = ucfirst($ln);

	}


?>

<!-- Add the wrapper -->
<div class="wrapper">


<!-- Add the header for the user -->
<div class="header_user">

	</div>


	<!-- The div for content -->
	<div class="content">

       <!-- The div for middle -->
	<div class="middle">


	<!-- The div for photo -->
	<div class="photo">


       <!-- End photo -->
	</div>


       <!-- The div for info -->
	<div class="info">


	<h1><?php echo "{$fn} {$ln}"?></h1>



	<?php if (isset($_SESSION['user_id']) == $id) 

	// Check that a file was uploaded...

	if (is_uploaded_file($_FILES['image']['tmp_name'])) {

		// Create a temporary file name...

		$temp = 'uploads/' . md5($_FILES['image']['name']);

		// Move the file over...

		if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) {

			$i = $_FILES['image']['name'];

		} else {

		}

	} else {

	}

	echo '



	<form enctype="multipart/form-data" action="profile.php" method="post">
	<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
	<p>Upload your photograph!   <input type="file" name="image" />
	<input type="submit" name="submit" value="Upload"/>
	<input type="hidden" name="submitted" value="TRUE"/>

	'

	?>

       <!-- End info -->
	</div>

       <!-- End middle -->
	</div>

       <!-- End content -->
	</div>

       <!-- End wrapper -->
	</div>

       <?php ; } ?>

 

Any help would be very much appreciated...

Link to comment
Share on other sites

Hi again,

 

And thanks for the replies. I have tried to debug in two ways:

 

1. I applied error_reporting('E_ALL') to the top of both the long/short script; and it printed the following notices:

 

a. Undefined index: image in .../users.php (this is the long script)

b. Undefined variable: id in .../users.php (this refers to the if (isset($_SESSIONS('user_id')))

 

However only the first notice comes up in the image.php script as well, which uploads to the directory perfectly.

 

2. I tried to echo out each 'if' like so (and nothing came out of this either). You can see that this code is a bit different from the one used in the scripts above (essentially I just moved the $_SESSIONS echo command a bit lower to apply only to the form being shown).

 

<?php 

	// Check that a file was uploaded...

	if (is_uploaded_file($_FILES['image']['tmp_name'])) {

		echo 'File uploaded!';

		// Create a temporary file name...

		$temp = 'uploads/' . md5($_FILES['image']['name']);

		// Move the file over...

		if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) {

			echo 'File moved!';

			$i = $_FILES['image']['name'];

		} else {

			echo 'File could not be moved!';

		}

	} else {

		echo 'No file has been uploaded';

	}

	if (isset($_SESSION['user_id']) == $id) {



	echo '



	<form enctype="multipart/form-data" action="user.php" method="post">
	<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
	<p>Upload your photograph!   <input type="file" name="image" />
	<input type="submit" name="submit" value="Upload"/>
	<input type="hidden" name="submitted" value="TRUE"/>

	'

	;}

	?>

 

I'm a bit stumped! Thanks for the help and apologies for not indicating what I'd done to remedy it beforehand.

Link to comment
Share on other sites

As for the error messages, the complete error messages would be best to see. When you say you "nothing came out" of adding the echo statements, what does that mean? You didn't see any of the echo statements being executed?

Link to comment
Share on other sites

Hello there,

 

Just wanted to tinker with it before I asked any more questions.

 

The error messages disappeared when I changed the image upload script to this (where if (isset($_POST['image'])) was not there before). I am well and truly confused now, because nothing seems to indicate what the problem is at all.

 

if (isset($_POST['image'])) {

	if (is_uploaded_file($_FILES['image']['tmp_name'])) {

		// Create a temporary file name...

		$temp = 'uploads/' . md5($_FILES['image']['name']);

		// Move the file over...

		if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) {

			$i = $_FILES['image']['name'];

		} else {

		}

	} else {

	}

	}

 

 

When I say that the echo statements didn't do anything, all that happens when I try and upload something is the page refreshes back to user.php. Nothing happens, even if I try to echo every statement out. I have also tried to:

 

1. Make sure the user.php (long script) works perfectly without any notices before I add in the image handling code. Everything seems fine.

 

2. Make sure that the image.php works perfectly without any notices (which it does and it uploads to the folder).

 

It's only when I add the image handling code to the long script that it suddenly doesn't work.

 

However, I tried using only this code to see if anything was actually being posted at all:

 

if (isset($_POST['image'])) {

 

echo 'Image submitted';

 

} else {

 

}

 

And all that happened was the page user.php refreshed itself blank without any errors. Is there some problem then with actually getting the image to be posted? Many thanks for your help...

Link to comment
Share on other sites

I apologize, as I don't have the book in front of me, but one thing I can say for sure, you're using POST to send the data, but trying to receive it with the GET superglobal, which won't work.

 

As for the SESSION superglobal not containing a user_id entry, etc., it's hard to comment on, because we don't know the exact configuration of your server. That doesn't mean you have to tell us, but what I would do is change the POST/GET issue, and then start echoing statements like mad throughout the script until you can locate the error.

 

Hope that helps.

  • Upvote 1
Link to comment
Share on other sites

Hi there! Thanks for your comment -- could you perhaps point me to which exact parts of my code you're referring to? I see that I'm posting the image data, but which GET superglobal am I using? Or am I missing something here?

Link to comment
Share on other sites

After looking at your scripts more closely, I honestly do not know what's going on and what you want to accomplish. If you don't mind, please explain exactly what you want, and we can help you through this. If you just want to make it possible to upload an avatar, I really don't think you need to check the user ID and all that.

 

Essentially, I'm not sure why you can't just use the first script, and slightly modify it to your needs. (By the way, your form in the first script is missing an end tag. I'm surprised that's not causing syntax errors.)

 

Anyway, please tell us exactly what you want, and we can help.

Link to comment
Share on other sites

I'm not sure this is causing the problem, but I just noticed your form is missing the closing HTML form tag. Doing a W3C validation on the HTML (of the page with the form) might be a good debugging step, too.

Link to comment
Share on other sites

Hello! Thank you for your replies. I added the closing tag, which must have been erased in the furious deletion and insertion, and no luck.

 

This is what I am trying to do so it is clear: the long script is essentially what will display a user profile in a larger community forum. The first three if statements refer to using $_GET to retrieve the right profile information from the URL's specification of the member ID... so that doesn't have anything to do with the problem.

 

Essentially, what I am trying to do is to insert a code in here that will also allow the person whose profile it is to be able to upload a photo as well when they are viewing their own profile.

 

I would really appreciate any help.

Link to comment
Share on other sites

I prefer not to do this, generally, but it's hard debugging this script because the problem (apparently the form isn't being submitted) is unusual, but how about you email me the current version of user.php (right?) and I'll take a look.

Link to comment
Share on other sites

Well, it looks like at this point, Larry will resolve the issue for you, but in general, I would do the following:

 

1) Add an if statement in your script to check whether the user of the profile being viewed is equal to the currently logged in user. Within the if statement, I'd add a link to a separate script for uploading an avatar image.

 

2) From the image upload script, I'd check one more time to make sure that the logged in user is in fact the same user. After testing that, I'd display a basic file upload form and more or less run the script the way Larry recommends.

 

By doing that, you can more or less use Larry's script as is, and just link to that script when appropriate.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...