Forum Moderators: coopster

Message Too Old, No Replies

Uploading Pictures

Inserting an image into a page

         

AliTaylor4411

11:32 pm on Aug 5, 2010 (gmt 0)

10+ Year Member



I have created a form below:

<html>
<head>
<title>Upload a File</title>
</head>
<body>
<form enctype="multipart/form-data" action="do_uploadmatt.php" method="POST">
<p><input type="hidden" name="MAX_FILE_SIZE" value="50000">
File to Upload: <input name="propertyimage" type="file"></p>
<p> Name for uploaded file: <input Name "filename" type="text" id="filename"
value="picture.jpg"></p>
<p><input type="submit" value="Upload!"></p>
</form>
</body>
</html>

and a php script below:

<?php
//Assign the entered filename to a variable
$filename = $_POST[filename];
//Specify the upload directory - assign to a variable
$uploaddir = '/home/ahtaylor1/ahtaylor1.bimserver2.com/images';
//Append the filename to the end of the upload directory
$uploadfile = $uploaddir . $filename;
//If upload is successful, print confirmation message
if (move_uploaded_file($_FILES['propertyimage']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.<br>";
echo "Its name is <a href=$filename>$filename</a>";
} else {
echo "There was an error.";
}
?>

Can anyone tell me why I just keep getting the "There was an error" message when I try to upload an image.

astupidname

5:21 am on Aug 6, 2010 (gmt 0)

10+ Year Member



A few small issues here, first off
<input Name "filename"

Should be
<input name="filename"


Then, the $uploaddir is invalid as it won't work to access by /home/accountname/subdomain.maindomain.com/etcetera
What you are looking for is the document root of the subdomain, typically a good thing to use as a constant in all scripts. So, after changing the input above, this should get you on your way:

<?php
if (!defined('ROOT')) {
define('ROOT', rtrim($_SERVER['DOCUMENT_ROOT'], '/')); //the rtrim is to ensure no slash at end (different server setups may/may not have it)
}
//Assign the entered filename to a variable
$filename = $_POST[filename];
//Specify the upload directory - assign to a variable
$uploaddir = ROOT.'/images';
//Append the filename to the end of the upload directory
$uploadfile = $uploaddir.'/'.$filename; //note the slash '/' needed here between the directory name and file name
//If upload is successful, print confirmation message
if (move_uploaded_file($_FILES['propertyimage']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.<br>";
echo "Its name is <a href=$filename>$filename</a>";
} else {
echo "There was an error.";
}
?>


Also, be sure to set permissions on the images folder to 777

Matthew1980

7:32 am on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

$_POST[filename];

Just to point out really.. This will make php throw an error: "Undefined index presumed constant" because you haven't put the global $_POST array key in quotes: $_POST['filename']; Do that and errors will go away - of course this is assuming as you have error_reporting(); turned on on your development machine ;-p Because if this was to be shipped to another server (meaning different ini settings) this will error - better to cure the problem now than headaches later.

error_reporting(E_ALL); at the top of every script you work on will and does make for better coding in the end...

and wouldn't it be better to have some error handling in the mix to, ie check for the form being actioned, and error if it's not, else you may end up with a blank screen & start to scratch you head (we have all been there!)

Simple things like this will help (you will need to add the name attribute to the submit button):-

(<input type="submit" name="submit" value="Upload this file!" />)

<?php
if (isset($_POST['submit']) && ($_POST['submit'] == "Upload this file!")){
//process request from form
}
else{
//error occurred redirect back to form
}
?>

You get the idea.

As for:-
>>Also, be sure to set permissions on the images folder to 777

you can do 755 'cos you don't want want to give full permissions to a folder, just read & write :) though that's up to you...

Hope that helps..

Cheers,
MRb

AliTaylor4411

5:12 pm on Aug 6, 2010 (gmt 0)

10+ Year Member



Hi astupidname,

I have amended my php script as per your message but still get the "There was an error uploading the file".

Im a bit confused with the root directory and upload directory. Is the root directory where the images are currently sitting on my computer and the upload directory is the image folder on the server(which is a sub directory of my folder where all my html and php files are? Sorry if stupid questions but am new to this!

This is my code as amended:

<?php
//the rtrim is to ensure no slash at end (different server setups may/may not have it)
if (!defined('ROOT')) {
define('ROOT', rtrim($_SERVER['C:\Users\Alison\Documents\1UNIVERSITY\BT2201\images'], '/'));
}

//Assign the entered filename to a variable
$filename = $_POST['filename'];

//Specify the upload directory - assign to a variable
$uploaddir = 'ahtaylor1.bimserver2.com\images';

//Append the filename to the end of the upload directory
$uploadfile = $uploaddir . $filename;

//If upload is successful, print confirmation message

if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile)) {

echo "File is valid, and was successfully uploaded. <br>";
echo "Its name is <a href=$filename>$filename</a>";
}
else
{
echo "There was an error in uploading the file.";
}
?>