uploading 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
digrev01
Forum Newbie
Posts: 22
Joined: Fri Oct 07, 2011 9:15 am

uploading error

Post by digrev01 »

hi eveybody im very confused because this code doesnt work i dont know whats is wr0ng... intresting thing is

it works just one equal sign but should be double




<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<input type="file" name="dosya" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>



<?php
$Kaynak=$_FILES["dosya"]["tmp_name"];
$Dsize=$_FILES["dosya"]["size"];
$ad=$_FILES["dosya"]["name"];
$tur=$_FILES["dosya"]["type"];
$hedef="./images";
if($tur=="image/png" || $tur=="image/gif" || $tur=="image/jpg")
{
$sonuc=move_uploaded_file($Kaynak,$hedef.'/'.$ad);
if($sonuc)
echo "ok";
}
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: uploading error

Post by social_experiment »

Code: Select all

<?php
$Kaynak=$_FILES["dosya"]["tmp_name"];
$Dsize=$_FILES["dosya"]["size"];
$ad=$_FILES["dosya"]["name"];
$tur=$_FILES["dosya"]["type"];
$hedef="./images";
if($tur=="image/png" || $tur=="image/gif" || $tur=="image/jpg")
{
$sonuc=move_uploaded_file($Kaynak,$hedef.'/'.$ad);
if($sonuc) echo "ok";
// add this to your code
else {
 echo $_FILES['doysa']['error'];
}
// --
}
?>
Add the bit of code above to your script, once the upload fails, an error code will be returned and you will have a place to start looking. You should also add a hidden field to the form named MAX_FILE_SIZE with the maximum allowed filesize (in kilobytes) that the user can upload. It can be bypassed but it's good practise to have it in place.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
digrev01
Forum Newbie
Posts: 22
Joined: Fri Oct 07, 2011 9:15 am

Re: uploading error

Post by digrev01 »

hi again if i change the code (this way like below) it works great but i cant understand why...could you explain please what was wrong
$hedef="./images";
if($tur == "image/jpeg" || $tur == "image/png" || $tur == "image/pjpeg" ||$tur == "image/bmp" )
{

move_uploaded_file($kaynak,$hedef.'/'.$ad);

}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: uploading error

Post by social_experiment »

Probably because of the extension .jpg. I've im not mistaken, the correct MIME type is image/jpeg or image/pjpeg as you have it now.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
digrev01
Forum Newbie
Posts: 22
Joined: Fri Oct 07, 2011 9:15 am

Re: uploading error

Post by digrev01 »

Yes my friend you are right because of MIME type thank yoıu for your help :))
Post Reply