/    Sign up×
Community /Pin to ProfileBookmark

Can’t upload my file properly

Hi,

i am having trouble trying to upload my .jpg file onto my server allong with it’s information about it e.g file description onto my db. The code bellow:

[code=php]

if(isset($_POST[‘action’]) and $_POST[‘action’] == ‘upload’)
{
$description = mysqli_real_escape_string($link, $_POST[‘description’]);
$category = mysqli_real_escape_string($link, $_POST[‘category’]);
$file = ($_FILES[‘photo’][‘name’]);

$target = ‘./images/’;
$target .= basename($_FILES[‘photo’][‘name’]);

// Checkk if a file was actually uploaded
if(!is_uploaded_file($_FILES[‘photo’][‘tmp_name’]) )
{
echo $error = ‘Error, there was no file uploaded’;
print_r($_FILES[‘photo’][‘tmp_name’]);

// include ‘error.html.php’;
exit();
}

if(file_exists($target))
{
$error = ‘This file already exists on the server’;
echo ($target);
include ‘error.html.php’;
exit();
}

if(move_uploaded_file($_FILES[‘photo’][‘tmp_name’],$target))
{
// Get information from the form
$uploaddesc = $_POST[‘description’];
$uploadcat = $_POST[‘category’];
$uploadname = $_FILES[‘photo’][‘name’];

//Prepare for user submitted for safe database insert
$uploaddesc = mysqli_real_escape_string($link, $uploaddesc);
$uploadcat = mysqli_real_escape_string($link, $uploadcat);
$uploadname = mysqli_real_escape_string($link, $uploadname);

include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/shanghai_db.inc.php’;
$sql = “INSERT INTO image_detail SET Filename = ‘$uploadname’,
Category = ‘$uploadcat’, Description = ‘$uploaddesc'”;

if(!mysqli_query($link, $sql) )
{
$error = ‘Database error storing file information’;
include ‘error.html.php’;
exit();
}
else
{

$output = ‘The file and information has been stored successfully’;
include ‘success.html.php’;
}

}
else
{
die(“Error moving file”);
}

}
[/code]

[CODE]
<form enctype=”multipart/form-data” action=” ” method=”post”>
<div>
<label for=”description”> Description: </label> <input type=”text” name=”description” id=”description” /> <br >
</div>

<div>
<label for=”category”> Category </label>
<select name=”category” id=”category”>
<option value=”Cheng_Huang_Temple”>Cheng Huang Temple </option>
<option value=”Shanghai_Zoo”>Shanghai Zoo </option>
</select> <br />
</div>

<div>
<label for=”uploadimg”> Upload Photo: </label>
<input type=”file” id=”photo” name=”photo”/> <br >
</div>

<div>
<input type=”hidden” name=”action” value=”upload” />
<input type=”submit” value=”Upload” />
</div>
</form>
[/CODE]

for some reason why i submit my file allong with it’s info, php seems to go nito my, $error = ‘Error, there was no file uploaded’; state and i don’t know why. I looked at the tmp_name to see i was writing it correctly, but it is correct.
Can somebody please tell me why it seems to go into my eeror state please.

Thx

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@BIOSTALLJul 03.2010 — Just out of curiosity, what happens if you put:

[code=php]print_r($_FILE);[/code]

Before you run the is_uploaded_file() function?
Copy linkTweet thisAlerts:
@tirnaJul 03.2010 — to help find the source of the problem, maybe add some error checking code at the top of your uplaod php script.

Maybe something like this.

[code=php]
//check if any errors occurred during the upload.

if($_FILES['txtUploadFile']['error'] > 0)
{
echo '<p>Problem: <br /></p>';
switch ($_FILES['txtUploadFile']['error'])
{
case 1: echo '<p>File exceeded upload_max_filesize.<br />'; break; //as specified in your php.ini
case 2: echo '<p>File exceeded max_file_size.<br />'; break; //exceeded max. file size as specified in your upload <form>
case 3: echo '<p>File only partially uploaded.<br />'; break;
case 4: echo '<p>No file uploaded.<br />'; break;
}

die();
}

[/code]
Copy linkTweet thisAlerts:
@nvidiaauthorJul 03.2010 — Just out of curiosity, what happens if you put:

[code=php]print_r($_FILE);[/code]

Before you run the is_uploaded_file() function?[/QUOTE]


I am would assume you are referring to $_FILES not $_FILE, but anyway, it outputs the following:

[CODE]
Array ( [photo] => Array ( [name] => IMG_0214.JPG [type] => [tmp_name] => [error] => 1 [size] => 0 ) )
[/CODE]


I'm guessing the [error] might be of use? If so, what does this mean?
Copy linkTweet thisAlerts:
@criterion9Jul 03.2010 — File exceeded upload_max_filesize.
Copy linkTweet thisAlerts:
@nvidiaauthorJul 03.2010 — Tirna, i just added your error checking and discovered that it outputed the:

Problem:


File exceeded upload_max_filesize.

which after seeing your comments, i discovered in my php.ini file that: upload_max_filesize = 2M which criterion9 was also right about. What i don't get is when i did print_r($_FILE);

why does

1) [type] => , not have a value as i would have thought to be something like .jpg,

2)[tmp_name] => also does not have a value as again i would have expected the file name.

3) [error] => 1 what does this mean exactly?
Copy linkTweet thisAlerts:
@nvidiaauthorJul 03.2010 — I found out what the [error] => 1 from the given link, which i found useful.

http://www.php.net/manual/en/features.file-upload.errors.php but not sure about the first two
Copy linkTweet thisAlerts:
@criterion9Jul 03.2010 — If an error occurs the rest of the results will not be what you'd expect as though the upload succeeded.
Copy linkTweet thisAlerts:
@tirnaJul 03.2010 — Tirna, i just added your error checking and discovered that it outputed the:

Problem:


File exceeded upload_max_filesize.

which after seeing your comments, i discovered in my php.ini file that: upload_max_filesize = 2M which criterion9 was also right about. What i don't get is when i did print_r($_FILE);

why does

1) [type] => , not have a value as i would have thought to be something like .jpg,

2)[tmp_name] => also does not have a value as again i would have expected the file name.

3) [error] => 1 what does this mean exactly?[/quote]


Error code 1 means that the size of the file you are trying to upload is larger than that allowed by the 'upload_max_filesize' parameter in your php.ini file.

If you have access to editing your php.ini file then maybe increase the value of upload_max_filesize or maybe try using ini_set() in your php script to set the upload_max_filesize limit.

Because the error code = 1, I would imagine no file was uploaded to your tmp directory and so there will be no value for 'type'.
Copy linkTweet thisAlerts:
@MindzaiJul 04.2010 — ...maybe try using ini_set() in your php script to set the upload_max_filesize limit...[/QUOTE]

You can't set upload_max_filesize via ini_set. You can set it in a .htaccess file though if you don't have access to php.ini (assuming .htaccess files are enabled of course).
Copy linkTweet thisAlerts:
@tirnaJul 04.2010 — [I] [I]...maybe try using [COLOR=#660000]ini_set()[/COLOR] in your php script to set the upload_max_filesize limit...[/I]

[/QUOTE]
[/I]
[I] [/I]

[I]yep, I wasn't sure if you could change it with ini_set() - hence the "maybe try.."[/I]

[I] [/I]

[I]upload_max_filesize doesn't appear in the[/I]

[I] list of parameters you can edit with ini_set()[/I]

I probably should have posted the link so the OP could check.
Copy linkTweet thisAlerts:
@nvidiaauthorJul 04.2010 — Since i'm using WAMP server, i thankfully have access to my php.ini file very easily, i changed the value from 2m to 10m as my .jpgs are a bit larger. It works now, thanks for all your help guys, much appreciated. ?
×

Success!

Help @nvidia spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...