Jump to content

image upload script


rugzo

Recommended Posts

Hi All,

 

i have a problem. I have a script which uploads and rezises the image directly to mysql. I implemented it to my registration form so the users can also upload and update their photo. My only problem is if the photo input field is blank (if the user doesn't want to upload his photo) the script stops with the error that the loaded file is not an image. Here is the script -->

 

 

include_once ('image.php');

$sinif = new imaj;

 

$tip = array (

"image/pjpeg",

"image/jpeg",

"image/jpg",

"image/gif",

"image/png",

"image/x-png"

);

 

 

 

 

 

if (!empty($_FILES['dosya']))

{

 

if (!in_array($_FILES['dosya']['type'],$tip))

{

exit('file is not an image!');

}

 

$sinif->img($_FILES['dosya']['tmp_name']);

$sinif->resize(130);

$sinif->store('tmp/'.$_FILES['dosya']['name']);

 

$icerik = base64_encode(file_get_contents('tmp/'.$_FILES['dosya']['name']));

$dosya_bilgi = getimagesize('tmp/'.$_FILES['dosya']['name']);

 

 

 

if ($query !== false)

{

 

if (unlink('tmp/'.$_FILES['dosya']['name']) == true)

{

 

}

  }

else

{

 

if (unlink('tmp/'.$_FILES['dosya']['name']) == true)

{

 

}

}

    }

 

 

Even ther is the --> if (!empty($_FILES['dosya']))

the script doesn't recognize that the field is empty and that it has to continue the next query.

I tried it also like ->

 

if(isset($_POST['dosya']))

or

if($_POST['dosya']<>'')

or

if($_POST['dosya'] !='').

 

I succeeded that with one of the aboves that it recognizes that the dosya field is empty and it also worked to continue with the rest of the query but then i saw that it is also not uploading the file when inserted. Can anyone help??

 

Link to comment
Share on other sites

You need to check the ['error'] element before you do anything with any of the uploaded file information - http://www.php.net/manual/en/features.file-upload.errors.php You will note that an error value of 4 indicates that no file was uploaded. A value of 0 indicates the file was uploaded without error. You should only process the uploaded file if the value is 0.

Link to comment
Share on other sites

Thanks for the quick reply but i don't have a problem with the file upload. If a file is inserted it uploads it without any problem. The only problem is if a file is not inserted, then it returns that the file is not an image. I just want this; it should check if a file is inserted. If the file is not inserted (it can be that the user wants the add his photo after he registered...) it should continue with the rest of the form and not stop with the error that the file is not an image -->

if (!in_array($_FILES['dosya']['type'],$tip))

  {

  exit('file is not an image!');

  }

I also do not want to remove this part since it checks the type of the file.

 

I don't know but i think that this part is not doing its job, it doesnt stop if the field is empty -->

 

if (!empty($_FILES['dosya']))

{

Link to comment
Share on other sites

$_FILES['dosya'] will only be empty for one of the following - uploads are not enabled on the sever, your form does not have enctype="multipart/form-data", or the size of the uploaded file exceeds post_max_size.

 

When a file is not selected in an upload form $_FILES['dosya'] is not empty. It in fact contains $_FILES['dosya']['error'] with a value of 4 like you were already told. By testing for $_FILES['dosya']['error'] == 0, before you use any of the unloaded file information, your code will at least not attempt to insert any of the information into the database unless a file was successfully uploaded.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.