...

View Full Version : Image upload problem.



Skiroid
09-01-2011, 06:22 PM
Hi, I'm working on my very first PHP/MySQL website and I'm quite excited about it: http://swiftsnap.skiroid.com/

However, one of my pages, front page, doesn't work as I'd like and am hoping one of you guys/gals could spot the fault within the code. I've checked it over numerous times but I just can't see what's going wrong.

Basically, I'm making an image uploading site and on the font page is a quick upload which checks whether the file selected is an image or not. If it is then it goes ahead and uploads it. Although, it's saying that images are not images and won't upload images.

I apologise if the code is messy or if there is unessential code but I am a noob when it comes to PHP.


<?php
$btnupload = $_POST['btnupload'];
$btnchoose = $_POST['btnchoose'];
$date = date("jS F Y, g:ia");
if ($_SESSION['LoginEmail']) {
if (isset($btnupload)) {
$name = $_FILES['picture']['name'];
$type = $_FILES['picture']['type'];
$tmpname = $_FILES['picture']['tmp_name'];
$ext = substr($name, strrpos($name, '.'));
if (strstr($type, "image")) {
$querysession = mysql_query("SELECT * FROM users WHERE LoginEmail='$sessionemail'");
while ($row = mysql_fetch_assoc($querysession)) {
$dbaccountid = $row['AccountID'];
}
$queryalbum = mysql_query("SELECT * FROM albums WHERE Name='Quick Upload' AND CreatedAccountID='$dbaccountid'");
while ($row = mysql_fetch_assoc($queryalbum)) {
$dbalbumid = $row['AlbumID'];
}
$charsetpictureid = '0123456789';
for ($i = 0; $i <= 10; $i++) {
$randpictureid = rand() % strlen($charsetpictureid);
$tmppictureid = substr($charsetpictureid, $randpictureid, 1);
$codepictureid .= $tmppictureid;
}
while ($numrows != 0) {
for ($i = 0; $i <= 10; $i++) {
$randpictureid = rand() % strlen($charsetpictureid);
$tmppictureid = substr($charsetpictureid, $randpictureid, 1);
$codepictureid .= $tmppictureid;
}
}
$charsetname = '0123456789';
for ($i = 0; $i <= 10; $i++) {
$randname = rand() % strlen($charsetname);
$tmpname = substr($charsetname, $randname, 1);
$codename .= $tmpname;
}
while ($numrows != 0) {
for ($i = 0; $i <= 10; $i++) {
$randname = rand() % strlen($charsetname);
$tmpname = substr($charsetname, $randname, 1);
$codename .= $tmpname;
}
}
mysql_query("INSERT INTO pictures VALUES ('$ccodepictureid','$dbalbumid','$codename','$name','$dbaccountid','$date')");
mkdir("album/$dbalbumid/");
move_uploaded_file($tmpname, "album/$dbalbumid/".$codename.$ext);
echo "
<div id='notification-yes'>
<div id='notification-yes-image'></div>
<h1>Your picture is ready to share!</h1>
<p>Your image has been uploaded to:<br /><a href='http://swiftsnap.skiroid.com/quickpic/$code$ext' target='_blank'>swiftsnap.skiroid.com/album/$dbalbumid/$codename$ext</a></p>
</div>
";
}
else
echo "
<div id='notification-no'>
<div id='notification-no-image'></div>
<h1>Unable to upload your file.</h1>
<p>You have either chosen a file which is not an image or you haven't chosen a file at all. You can only upload images to Swift Snap.</p>
</div>
";
}
else
echo "
<div id='notification-ok'>
<div id='notification-ok-image'></div>
<h1>Choose your image to upload.</h1>
<p>Swift Snap offers you a quick and easy service. All you have to do is choose an image you'd like to upload and you'll have the link to share it within seconds!</p>
</div>
";
}
else
if (isset($btnupload)) {
echo "
<div id='notification-no'>
<div id='notification-no-image'></div>
<h1>You need to be logged in.</h1>
<p>You are required to log in to Swift Snap to start uploading your pictures. Registration is free and should take no longer than a minute.</p>
</div>
";
}
else
echo "
<div id='notification-ok'>
<div id='notification-ok-image'></div>
<h1>Choose your image to upload.</h1>
<p>Swift Snap offers you a quick and easy service. All you have to do is choose an image you'd like to upload and you'll have the link to share it within seconds!</p>
</div>
";
?>

Thanks. :o

Skiroid
09-01-2011, 06:50 PM
Could you explain please? I'm not too good with PHP sorry.

Rowsdower!
09-01-2011, 06:50 PM
Have you checked your $_FILES array to make sure you are using the correct keys?

If $type is correct (if it has the mime type of the uploaded file) then there isn't any reason I can see why this shouldn't work...

In other words, if $type contains any reference to the term "image" then this should be working (or at least it should NOT be rejecting valid images).

Skiroid
09-01-2011, 06:52 PM
Have you checked your $_FILES array to make sure you are using the correct keys?

If $type is correct (if it has the mime type of the uploaded file) then there isn't any reason I can see why this shouldn't work...

In other words, if $type contains any reference to the term "image" then this should be working (or at least it should NOT be rejecting valid images).

$_FILES array?

Rowsdower!
09-01-2011, 06:57 PM
Add this to the top of the script, just under the "<?php" part:


print_r($_FILES);

Then go to your main page and upload a test image. When the page loads you will see a bunch of text looking something like this (variable names and number of files will be different, but generally it will look like this):


Array
(
[download] => Array
(
[name] => Array
(
[file1] => MyFile.txt
[file2] => MyFile.jpg
)

[type] => Array
(
[file1] => text/plain
[file2] => image/jpeg
)

[tmp_name] => Array
(
[file1] => /tmp/php/php1h4j1o
[file2] => /tmp/php/php6hst32
)

[error] => Array
(
[file1] => UPLOAD_ERR_OK
[file2] => UPLOAD_ERR_OK
)

[size] => Array
(
[file1] => 123
[file2] => 98174
)
)
)

Copy what is there and paste it here for me to see...

I suspect you have an error in your handling of the $_FILES array but I won't know how to tell you to fix it until I see what's there...

Skiroid
09-01-2011, 07:01 PM
Add this to the top of the script, just under the "<?php" part:


print_r($_FILES);

Then go to your main page and upload a test image. When the page loads you will see a bunch of text looking something like this (variable names and number of files will be different, but generally it will look like this):


Array
(
[download] => Array
(
[name] => Array
(
[file1] => MyFile.txt
[file2] => MyFile.jpg
)

[type] => Array
(
[file1] => text/plain
[file2] => image/jpeg
)

[tmp_name] => Array
(
[file1] => /tmp/php/php1h4j1o
[file2] => /tmp/php/php6hst32
)

[error] => Array
(
[file1] => UPLOAD_ERR_OK
[file2] => UPLOAD_ERR_OK
)

[size] => Array
(
[file1] => 123
[file2] => 98174
)
)
)

Copy what is there and paste it here for me to see...

I suspect you have an error in your handling of the $_FILES array but I won't know how to tell you to fix it until I see what's there...

=> /tmp/phpXQmDm2 [error] => 0 [size] => 562204 ) )

Skiroid
09-01-2011, 07:07 PM
It used to work fine but I don't know what's causing it to go wrong now.

Rowsdower!
09-01-2011, 07:16 PM
=> /tmp/phpXQmDm2 [error] => 0 [size] => 562204 ) )

This is not complete. Can you please double check your output and make sure that the entire string is there?

Skiroid
09-01-2011, 07:22 PM
This is not complete. Can you please double check your output and make sure that the entire string is there?

I've put it here. The PHP code has HTML around it but I don't know if that effects anything.


<?php
print_r($_FILES);
$btnupload = $_POST['btnupload'];
$btnchoose = $_POST['btnchoose'];
$date = date("jS F Y, g:ia");
if ($_SESSION['LoginEmail']) {
if (isset($btnupload)) {

Then it comes out with: => /tmp/phpUNIUMu [error] => 0 [size] => 562204 ) )

http://swiftsnap.skiroid.com/Screenshot.png

Rowsdower!
09-01-2011, 07:30 PM
Sorry, I completely forgot that you gave us a link to the page. I ran one myself and this is what shows up in the source code:


Array
(
[btnchoose] => Array
(
[name] => xxxxxxxxxxxxxxxx.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php96j3iW
[error] => 0
[size] => 52557
)

)


Now, what we want to access will be under $_FILES['btnchoose']['type']

What you have in your code is $_FILES['picture']['type']

You need to change the word "picture" to "btnchoose" and this should work again. If you recently changed the name of your file input that's all it would take for this to have stopped working.

Skiroid
09-01-2011, 07:33 PM
Sorry, I completely forgot that you gave us a link to the page. I ran one myself and this is what shows up in the source code:


Array
(
[btnchoose] => Array
(
[name] => xxxxxxxxxxxxxxxx.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php96j3iW
[error] => 0
[size] => 52557
)

)


Now, what we want to access will be under $_FILES['btnchoose']['type']

What you have in your code is $_FILES['picture']['type']

You need to change the word "picture" to "btnchoose" and this should work again. If you recently changed the name of your file input that's all it would take for this to have stopped working.

Ahh, thanks. Yeh I tried cleaning the code up a bit thanks again! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum