Jump to content

[SOLVED] Need some help with uploading


pneudralics

Recommended Posts

The below is what I use in my isset. I can't figure out how to not allow upload if 1 of 3 doesn't work. For example if the main image doesn't upload I don't want the thumbnail to upload or the code to write to the database vice versa.

 

I tried doing an if (!empty).. to the $_Post but that didn't work. It adds the tmp name to the database.

 

//Check for main image

if (move_uploaded_file ($_FILES['image']['tmp_name'], GALLERYUPLOAD."$newname2")) {
	echo '<font color="red">Main image uploaded successfully.</font><br />';
} else {
	echo '<font color="red">Problem uploading main image.</font><br />';
}

//Check for thumbnail image

if (move_uploaded_file ($_FILES['timage']['tmp_name'], GALLERYTHUMBNAILUPLOAD."$newname2")) {
	echo '<font color="red">Thumbnail image uploaded successfully.</font><br />';
} else {
	echo '<font color="red">Problem uploading thumbnail image.</font><br />';
}

//End image upload
if	(mysql_query("INSERT INTO gallery (image) VALUE ('$newname2')")) {
	echo '<font color="red">The images have been written to the database successfully.</font><br /><br />';
} else {
	echo '<font color="red">Images did not write to the database.</font><br /><br />';
}

Link to comment
https://forums.phpfreaks.com/topic/130416-solved-need-some-help-with-uploading/
Share on other sites

you can use a flag in each if statement and set it to true or false depending on the result of the if statement and check it into the next if statement like:

//Check for main image

   if (move_uploaded_file ($_FILES['image']['tmp_name'], GALLERYUPLOAD."$newname2")) {
      echo '<font color="red">Main image uploaded successfully.</font><br />';
  $err_flag1 = true;
   } else {
      echo '<font color="red">Problem uploading main image.</font><br />';
   }

//Check for thumbnail image
if ($err_flag1 = true){
   if (move_uploaded_file ($_FILES['timage']['tmp_name'], GALLERYTHUMBNAILUPLOAD."$newname2")) {
      echo '<font color="red">Thumbnail image uploaded successfully.</font><br />';
  $err_flag2 = true;
   } else {
      echo '<font color="red">Problem uploading thumbnail image.</font><br />';
   }
}
}
else {
echo "Image not uploaded";
exit();
}
//End image upload
if ($err_flag2 = true){
   if   (mysql_query("INSERT INTO gallery (image) VALUE ('$newname2')")) {
      echo '<font color="red">The images have been written to the database successfully.</font><br /><br />';
   } else {
      echo '<font color="red">Images did not write to the database.</font><br /><br />';
   }
else{
echo "Image not uploaded";
exit();
}

 

I think that should do it

Tried it and it still doesn't work. I tried it several other ways. Currently have it like the below:

//Set main image, thumbnail and writing image name to database to true
$mainimagecheck = true;
$thumbnailimagecheck = true;
$databaseimagecheck = true;

//Check for main image
if ($thumbnailimagecheck = true) {
if ($mainimagecheck) {
	if (move_uploaded_file ($_FILES['image']['tmp_name'], GALLERYUPLOAD."$newname2")) {
		echo '<font color="red">Main image uploaded successfully.</font><br />';
	} 
} else {
	echo '<font color="red">Problem uploading main image.</font><br />';
}
} else {
echo '<font color="red">Main image didn\'t upload because thumbnail image didn\'t upload.</font><br />';
exit (); 
}
//Check for thumbnail image
if ($mainimagecheck = true) {
if ($thumbnailimagecheck) {
	if (move_uploaded_file ($_FILES['timage']['tmp_name'], GALLERYTHUMBNAILUPLOAD."$newname2")) {
		echo '<font color="red">Thumbnail image uploaded successfully.</font><br />';
	} 
} else {
	echo '<font color="red">Problem uploading thumbnail image.</font><br />';
}
} else {
echo '<font color="red">Thumbnail image didn\'t upload because main image didn\'t upload.</font><br />';
exit ();
}
//End image upload
//If inputs empty do not load the database
if ($mainimagecheck && $thumbnailimagecheck) {
if	(mysql_query("INSERT INTO gallery (image) VALUE ('$newname2')")) {
	echo '<font color="red">The images have been written to the database successfully.</font><br /><br />';
} else {
	echo '<font color="red">Images did not write to the database.</font><br /><br />';
}
} else {
echo '<font color="red">Images did not write to the database because main or thumbnail didn\'t upload.</font><br /><br />';
exit ();
}

Or, you could just nest your if statements properly. Consider the following pseudo code:

 

if(file is uploaded){
    //create the thumbnail
    if(thumbnail is created){
        //try to execute the query
        if(query is successful){
            //everything worked
        }else{
            //query failed
        }
    }else{
        //thumbnail not created
    }
    
}else{
    //file not uploaded
}

 

Also, to create a thumbnail, you should be resizing the file that's already been uploaded.

Or, you could just nest your if statements properly. Consider the following pseudo code:

 

if(file is uploaded){
    //create the thumbnail
    if(thumbnail is created){
        //try to execute the query
        if(query is successful){
            //everything worked
        }else{
            //query failed
        }
    }else{
        //thumbnail not created
    }
    
}else{
    //file not uploaded
}

 

Also, to create a thumbnail, you should be resizing the file that's already been uploaded.

 

Thanks I was just thinking about that. Do you know of a good tutorial that will teach that without using any other utilities? I would like to have just one box to upload and automatically create a thumbnail.

Can't recommend a particular one i'm afraid, though if you google you'll find loads. If you want to give it a go yourself, then you'll need to use the imagecopyresampled() function on the uploaded image to create a thumbnail.

 

Edit: In fact, if you look at the examples given on that page you'll get a pretty good idea of what to do.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.