PDA

View Full Version : PHP image upload help


Dino173
11-01-2012, 05:04 PM
Hi all, i have been having trouble with uploading an image from a form, using this images location to store it into MySQL.

<?php

// Include database connection file
include('../inc/connection.inc.php');

// Check to see if the form has been submitted
if (isset($_POST['submit']))
{
//This is the directory where images will be saved
$target = "../images/avatars";
$target = $target . basename($_FILES['photoLocation']['name']);

// Check to see all fields have been completed
$photoLocation =($_FILES['photoLocation']['name']);
$name = $_POST['name'];
$reason = $_POST['reason'];
$whatPub = $_POST['whatPub'];
$expire = $_POST['expire'];

if (!empty($photoLocation) && !empty($name) && !empty($reason)&& !empty($whatPub)&& !empty($expire))
{
// Create an SQL query to add the comment
$sql = "INSERT INTO tblPhotoUpload (photoLocation, name, reason, whatPub, expire) VALUES ('$photoLocation', '$name', '$reason', '$whatPub', '$expire')";


// Connect to the database
connect();

// Run the query and store the result in a variable
$result = mysql_query($sql) or die("Could not run query");

// Close connection to the database
mysql_close();

//Writes the photo to the server
if(move_uploaded_file($_FILES['photoLocation']['tmp_name'], $target))

// Check if query was successful


if ($result)
{
$message = '<div class="success"><p>Thank you for your upload.</p><p>Please <a href="./index.php">Click Here</a> to view the uploads</p></div>';
}
else
{
$message = '<div class="error"><p>There was an error adding your upload, please try again</p></div>';
}
}
else
{
$message = '<div class="error"><p>Please make sure you fill all fields in before submitting the form.</p></div>';
}
}
?>

<p>Please enter the banned person and click submit</p>

<form enctype="multipart/form-data" name="addBanderPerson" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

<?php
if (isset($message))
{
echo $message;
}
?>

<div class="label"><label for="photoLocation">Upload a Photo:</label></div>
<div class="input"><input type="file" name="photoLocation" id="photoLocation" tabindex="1" /></div>
<br />
<div class="label"><label for="name">Name:</label></div>
<div class="input"><input type="text" name="name" id="name" tabindex="2" /></div>
</div>
<br />
<div class="label"><label for="reason">Reason:</label></div>
<div class="input"><input type="text" name="reason" id="reason" tabindex="3" /></div>
<br />
<div class="label"><label for"whatPub">Waht Pub:</label></div>
<div class="input"><input type="text" name="whatPub" id="whatPub" tabindex="4" /></div>
<br />
<div class="label"><label for"expire">Expire:</label></div>
<div class="input"><input type="text" name="expire" id="expire" tabindex="5" /></div>
<br />
<div class="input">
<input type="reset" name="reset" id="reset" value="Reset" tabindex="6" />
<input type="submit" name="submit" id="submit" value="Submit" tabindex="7" />
</div>

</form>

the error message i get when running it is.


Warning: move_uploaded_file(../images/avatarsChrysanthemum.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/dsmith/public_html/HebburnAndJarrowPubWatch/admin/addBanderPerson.php on line 36

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpHPUyXz' to '../images/avatarsChrysanthemum.jpg' in /home/dsmith/public_html/HebburnAndJarrowPubWatch/admin/addBanderPerson.php on line 36

any help or suggestions would be great.

edbr
11-02-2012, 01:20 AM
check your permissions on the folders , they should have write permission

Dino173
11-02-2012, 10:32 AM
Thanks for the reply.

I have checked the permissions and i have full access to the folders and sub folders.