Jump to content

How do i skip a $_FILES in a form post?


JJohnsenDK

Recommended Posts

Hey

 

Im wondering how i skip the $_FILES part in a form post. I need this because its not every time i need to submit with a picture in my form.

 

Here is the code im using:

 

<?php
foreach($_FILES['image']['error'] as $k => $error){

					//Tjekker for fejl, hvis igen gå videre
					if($_FILES['image'][$k][$error] == 0){

						//Tjekker om billedet er et gyldigt billede. JPEG eller GIF billede.
						if($_FILES['image']['error'][$k] == "image/jpeg" || $_FILES['image']['error'][$k] == "image/pjpeg" || $_FILES['image']['error'][$k] == "image/gif"){

							if(!empty($_FILES['image']['name'][$k])){
								if(empty($_POST['order'])){
									echo "Du skal vælge hvilket nr. billedet skal være. 1, 2 eller 3.";
								}else{
									//Kopier billedet over i den angivne mappe
									copy($_FILES['image']['tmp_name'][$k], "images/products/".strtolower(get_type_name($_POST['type']))."/".strtolower(get_cat_name($_POST['cat']))."/".$_FILES['image']['name'][$k]."");
									echo $lan['007'].$_FILES['image']['name'][$k];
}
							}
						}else{
							echo "Billedet skal være et JPEG billede.";
						}
					}else{
						echo "Der skete en fejl da billedet skulle uploades. Prøv igen.";
					}
?>

 

I thougth i could do this:

if(empty($_FILES['image']['name'])){

  then skip the hole foreach part

}

 

but this didnt help.

 

What to do??

Link to comment
Share on other sites

What type of form is it?

 

If it's like a post where there is a box to post the image URL, then you can just set up an if statement.

 

<form action='$_SERVER['PHP_SELF']' method='post'>
  <input type='text' size='20' name='picURL' />
</form>
<?php

$picURL = $_POST['picURL'];
if ($picURL)
   // code to upload pic
else
   // exit

?>

 

Something like that?

Link to comment
Share on other sites

<?php


if($_FILES['image']==NULL)
{
foreach($_FILES['image']['error'] as $k => $error){

					//Tjekker for fejl, hvis igen gå videre
					if($_FILES['image'][$k][$error] == 0){

						//Tjekker om billedet er et gyldigt billede. JPEG eller GIF billede.
						if($_FILES['image']['error'][$k] == "image/jpeg" || $_FILES['image']['error'][$k] == "image/pjpeg" || $_FILES['image']['error'][$k] == "image/gif"){

							if(!empty($_FILES['image']['name'][$k])){
								if(empty($_POST['order'])){
									echo "Du skal vælge hvilket nr. billedet skal være. 1, 2 eller 3.";
								}else{
									//Kopier billedet over i den angivne mappe
									copy($_FILES['image']['tmp_name'][$k], "images/products/".strtolower(get_type_name($_POST['type']))."/".strtolower(get_cat_name($_POST['cat']))."/".$_FILES['image']['name'][$k]."");
									echo $lan['007'].$_FILES['image']['name'][$k];
}
							}
						}else{
							echo "Billedet skal være et JPEG billede.";
						}
					}else{
						echo "Der skete en fejl da billedet skulle uploades. Prøv igen.";
					}

}
?>

 

 

This should work, but with no real way to test (and my poor german skills, lol), I can't be entirley sure...no reason why it won't though.

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.