Jump to content

PHP script is not uploading to directory


mdmartiny

Recommended Posts

I have been working on a script for someone and I have hot a road block. Someone was helping me and they rewrote my script and now I am lost.

 

The script is supposed to upload images to a directory that I have set up on my server. After Submission display the images for the user.

 

It does not upload images to the directory I have set up.

 

<?PHP

session_start();

if(isset($_POST['title'])) $title = $_POST['title'];
else echo "<p>Title was not set...</p>";
if(isset($_POST['year'])) $year = $_POST['year'];
else echo "<p>Year was not set...</p>";
if(isset($_POST['make'])) $make = $_POST['make'];
else echo "<p>Make was not set...</p>";
if(isset($_POST['model'])) $model = $_POST['model'];
else echo "<p>Model was not set...</p>";
if(isset($_POST['model'])) $descript = $_POST['descript'];
else echo "<p>Description was not set...</p>";
if(isset($_POST['max_no_img'])) $max_files = $_POST['max_no_img']; 
else echo "<p>Max Number of Images was not set...</p>";

if($title=="") echo "<p>Title is empty...</p>";
if($year=="") echo "<p>Year is empty...</p>";
if($make=="") echo "<p>Make is empty...</p>";
if($model=="") echo "<p>Model is empty...</p>";
if($descript=="") echo "<p>Description is empty...</p>";
if($max_files=="") echo "<p>Files are empty...</p>";

$valid_types = array ("image/gif", "image/jpg", "image/jpeg", "image/bmp",  "image/png");
$max_size = 2000000;
$path = "../CMS/db_images/"; ------ This is the database that I have set up. Images do not go here. This displays the images after they have been submitted. 
$i=0;

require('includes/connection.php');

while($i<$max_files) {	
if ($_FILES['images']['name'][$i] != '') { /* check if file name field empty or not */		
	if (in_array($_FILES['images']['type'][$i], $valid_types)) { /* check for valid image type */							 			if($_FILES['images']['size'][$i]<=$max_size) { /* check for allowed size */				
			$check_name = $_FILES['images']['name'][$i];				
			$check_name = preg_replace("/[^a-zA-Z0-9\.]/", "", $check_name);
				$good = 0;				
					while($good==0) {					
						if(!file_exists($path . $check_name)) { /* check file that file name already exists */						
						/* move the file */ -------- This does not move them into the directory at all					
							if (copy($_FILES['images']['tmp_name'][$i], $check_name)) {																						 									$all_images = $all_images . $check_name . ",";
								$good=1;
						}else{							
						?>
							CHECK WRITE PERMISSIONS OF IMAGE FOLDER!<br>
							<a href="admin_menu.php">Return to main menu</a>
						<?PHP
                            	exit();
						}
				}else{ /* create new name */						
					$check_name = date("YmdHis") . $_FILES['images']['name'][$i];	
					$check_name = preg_replace("/[^a-zA-Z0-9\.]/", "", $check_name);
				}	
			}			
		}else{				
		?>
					FILE SIZE IS TO LARGE. REZIZE IMAGE!<br>
					<a href="admin_menu.php">Return to main menu</a>
					<?PHP
                        exit();			
		}		
			}else{			
		?>
					FILE SIZE IS TO LARGE. REZIZE IMAGE!<br>
                        <a href="admin_menu.php">Return to main menu</a>
		<?PHP			
					exit();		
			}	

			}else{		
			/* in the event user uses SOME of the image fields and leaves others blank, we should NOT die on this condition */	
			}	
				$i ++;
		}$all_images = rtrim($all_images, ",");

$sql = "INSERT INTO $table (id, title, year, make, model, descript, image) VALUES (NULL, '$title', '$year', '$make', '$model', '$descript', '$all_images')";

$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Classified Added</title>
</head>
<body>
<div>
   <h3>The following information has been added to the <?php echo "$table"; ?></h3>
   <p> <strong>Title:</strong> <?php echo "$title"; ?> </p>
   <p> <strong>Year:</strong> <?php echo "$year"; ?> </p>
   <p> <strong>Make:</strong> <?php echo "$make"; ?> </p>
   <p> <strong>Model:</strong> <?php echo "$model"; ?> </p>
   <p> <strong>Description:</strong> <?php echo "$descript"; ?> </p>
   <?PHP
   if(substr_count($all_images, ',')>0) {	
   			$lines = explode(",", $all_images);	$num_images = count($lines);
			}else{	$lines[0] = $all_images;	
		$num_images = 1;}$i=0;while($i<$num_images) {	
	?>
   <p><strong>Image 
   <?PHP echo $i +1; ?>:</strong> <img src="<?PHP echo $path . $lines[$i]; ?>"></p>
   <?PHP	$i ++;}?>
   <p><a href="show_add.php">Add another classified ad</a></p>
   <p><a href="admin_menu.php">Return to main menu</a></p>
</div>
</body>
</html>

:hail_freaks:

Link to comment
Share on other sites

Well first of all you should probably put some error handling in there so that you can see where the problem is actually occuring at. But it seems to me that the problem is on the line that says "if(copy($_FILES['images']['tmp_name'][$i],$check_name)) {". You have not uploaded the file yet so you cant move it. You have to do move_uploaded_file so it should look like "if(move_uploaded_file($_FILES['images']['tmp_name'][$i],$check_name)) {". This may fix your problem and if not put in the error handling and let us know where the script is actually dieing at.

Link to comment
Share on other sites

Also looking at where you are trying to save the file where the move_uploaded_file part is it just has $check_name. This should be the directory to save it in plus the name. So it should be something like $_SERVER['DOCUMENT_ROOT'].'/'.$check_name. Of course that would be putting in the main root folder but you could try it to test. Also the enctype should be multipart/form-data if you are wanting to upload files with a form.

Link to comment
Share on other sites

All right doing that placed the image in the root directory... How would I go about putting it in /CMS/db_images/?

 

Sorry after I posted this I tried something and I figured it out....Thanks for all of your help. I appreciate it.

 

 

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.