move_uploaded_file Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

move_uploaded_file Error

Post by carleihar »

So I FINALLY was able to get this script to echo 'GREAT!', but now I'm worried that my image was actually uploaded. If it worked, shouldn't there be an image inside my uploads/riderimages file?

Here's the code: (not the whole thing, but the important part)

Code: Select all

<?

require_once('mysqli_connect.php');

$today = date("m-d-Y");

if (isset($_POST['submit'])) { // Handle the form.



if (isset($_FILES['image1'])) {

$allowed = array('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');

if(in_array($_FILES['image1']['type'], $allowed)) {

if (move_uploaded_file($_FILES['image1']['tmp_name'], "uploads/riderimages/{$_FILES['image1']['name']}")) {


/************* EVERYTHING IS GOOD TO GO ****************/
echo 'Great!';
				
/*$q = "INSERT INTO users (email, pass, username, active, barn_name, registration_date) VALUES ('$e', SHA1('$p'), '$un', '$a', '$bn', NOW() )";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

echo '<p>Thanks! You\'ve been added!</p>';
echo '<p>Go to the <a href="viewriders.html" alt="View Riders">riders</a> page.</p>';

} else {

echo '<p>There was a problem adding you to the rider database. Try again or contact Caroline.</p>';

}*/

} else {

echo '<p class="error">There was an error uploading your first image. Please contact Caroline.</p>';

}


} else { //not a valid type
echo '<p class="error">Sorry, that\'s not a valid image type. Please use only JPEG or PNG.</p>';	
}

} else { //there isnt anything in image1

echo '<p class="error">Please upload two image files.</p>';

}



}//submitted

if (file_exists($_FILES['image1']['tmp_name']) && is_file($_FILES['image1']['tmp_name']) ) {
unlink ($_FILES['image1']['tmp_name']);
}


?><title>Untitled Document</title>
<div id="textcontainer">
<h1>Add A Rider</h1>
<form action="" method="post" enctype="multipart/form-data">
<table>

<tr><td style="text-align:right">Image one:</td><td><input type="file" name="image1" id="image1"  /></td></tr>
<tr><td style="text-align:center" colspan="2"><input type="submit" name="submit" id="submit" value="Add" /></td></tr>
</table>
</form>
</div>

Is there a problem in my code or some security complications I should know about? This is my first time trying to upload a file. Any help would be great!

Also, as a side note, how would I store the image file in the database? Would I use $_FILES['image1']['name'] ?
collette
Forum Newbie
Posts: 10
Joined: Thu Mar 15, 2007 1:23 pm

Re: move_uploaded_file Error

Post by collette »

if the upload is successful there should be an image in "uploads/riderimages". But there are a lot of thing that can go wrong and you have to test for that. The following code is probably not perfect also, but it will upload images to the destination folder.

Code: Select all

<?php
// set the destination
$uploaddir="uploads/riderimages";

// test for errors
if ($_FILES['image1']['error'] > 0) {
	echo "There is a problem: ";
	switch ($_FILES['image1']['error']) {
		case 1:  echo "File greater as allowed in html file"; break;
		case 2:  echo "File greater as allowed in php.ini"; break;
		case 3:  echo "File only partial recieved"; break;
		case 4:  echo "No file recieved"; break;
	}
	exit;
}

$allowed = array('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (!in_array($_FILES['image1']['type'], $allowed)) {
	echo "You are only allowed to upload jpeg or png images";
	exit;
}

// set full path + filename
$upfile = $uploaddir . $_FILES['image1']['name'];

// check the actual upload
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
	if (!move_uploaded_file($_FILES['file']['tmp_name'], $upfile)) {
		echo "Unable to move file to destination.";
		exit;
	}
}
else {
	echo "there was a problem uploading your file.";
	exit;
}

// file is uploaded in $uploaddir
echo "<p>File upload succesvol</p>"
?>
Would I use $_FILES['image1']['name'] ?
Yes. This will give you the original name of the file.
jimmy patel
Forum Newbie
Posts: 3
Joined: Sun May 16, 2010 12:14 am

Re: move_uploaded_file Error

Post by jimmy patel »

Yes, code will work fine with PHP move_upload_file.

MoonRose Infotech
W: http://www.moonroseinfotech.com/
Post Reply