upload photo - php&mysql

Hello, I have a problem with image upload.
Normal script should upload the image and modify the table in mysql.

But when I upload the image, it does not up on the server and do not write rows in mysql.
The script is for image & virtual tour, my priority is upload photo, not virtual tour.

PLEASE…HELP :frowning:

This is php & html code:

[embed=425,349]<?php
require_once(‘includes/config.php’);
include ‘db/connect.php’;
include ‘db/functions.php’;

if(!isset($_SESSION[‘login’])) $_SESSION[‘login’] = ‘No’;
if($_SESSION[‘login’] != ‘Yes’)
{
echo ‘’;}
else
{

if(isset($_GET[‘id’])){$last_id=$_GET[‘id’];}
$error_image=’’;
$error_tur=’’;

if(isset($_POST[‘action’]) AND $_POST[‘action’]==‘load’){
$directory="./upload_photos/$last_id";
$filesize1=$HTTP_POST_FILES[‘ufile1’][‘size’][0];
$filesize2=$HTTP_POST_FILES[‘ufile2’][‘size’][0];
$path1= “…/admin/upload_photos/$last_id/images/”.$HTTP_POST_FILES[‘ufile1’][‘name’][0];
$path2= “…/admin/upload_photos/$last_id/virtual_tour/”.$HTTP_POST_FILES[‘ufile2’][‘name’][0];
$path3= “…/admin/upload_photos/$last_id/thumbnails/”.$HTTP_POST_FILES[‘ufile1’][‘name’][0];
if(!file_exists($directory))
{
mkdir("…/admin/upload_photos/$last_id");
chmod("…/admin/upload_photos/$last_id", 0777);
mkdir("…/admin/upload_photos/$last_id/images");
chmod("…/admin/upload_photos/$last_id/images", 0777);
mkdir("…/admin/upload_photos/$last_id/thumbnails");
chmod("…/admin/upload_photos/$last_id/thumbnails", 0777);
mkdir("…/admin/upload_photos/$last_id/virtual_tour");
chmod("…/admin/upload_photos/$last_id/virtual_tour", 0777);
}
if(isset($HTTP_POST_FILES[‘ufile1’][‘name’][0]) AND ($HTTP_POST_FILES[‘ufile1’][‘name’][0]!=’’))
{ if (!file_exists("…/admin/upload_photos/$last_id/images/".$HTTP_POST_FILES[‘ufile1’][‘name’][0])
AND ($filesize1!=0))
{
if(‘jpg’== strtolower(substr($HTTP_POST_FILES[‘ufile1’][‘name’][0], strrpos($HTTP_POST_FILES[‘ufile1’][‘name’][0], ‘.’) + 1)))

					{		
					$query0 =(mysql_query( "SELECT * FROM imagini WHERE id2='$last_id'"));
					if (mysql_num_rows($query0) < 1) 
									{	
									//copy file to where you want to store file
									include 'watermark.php';
									include 'thumbnails.php';

								
								/////////////////////////////////////
								//copy($HTTP_POST_FILES['ufile1']['tmp_name'][0], $path1);
								insertUploadImage($last_id,$path1,$path3,1);
								$error_image="Image <strong> ".$HTTP_POST_FILES['ufile1']['name'][0]." </strong> was loaded successfully <br/>";	
									}
					else{
						include 'watermark.php';
						include 'thumbnails.php';
						//copy($HTTP_POST_FILES['ufile1']['tmp_name'][0], $path1);
						insertUploadImage($last_id,$path1,$path3,0);
						$error_image="Image <strong> ".$HTTP_POST_FILES['ufile1']['name'][0]." </strong> was loaded successfully <br/>";
					}
					
					}
			else {$error_image="Only .jpg  extension<br/>";}
			}
else {$error_image="Image with name <strong> ".$HTTP_POST_FILES['ufile1']['name'][0]." </strong> exist on server<br/>";}  
}

if(!file_exists($directory))
{
	mkdir("../admin/upload_photos/$last_id");	
	chmod("../admin/upload_photos/$last_id", 0777); 
	mkdir("../admin/upload_photos/$last_id/images");
	chmod("../admin/upload_photos/$last_id/images", 0777); 
	mkdir("../admin/upload_photos/$last_id/thumbnails");
	chmod("../admin/upload_photos/$last_id/thumbnails", 0777); 
	mkdir("../admin/upload_photos/$last_id/virtual_tour");
	chmod("../admin/upload_photos/$last_id/virtual_tour", 0777);
}
if(isset($HTTP_POST_FILES['ufile2']['name'][0]) AND ($HTTP_POST_FILES['ufile2']['name'][0]!='') )
	{if (!file_exists("../admin/upload_photos/$last_id/virtual_tour/".$HTTP_POST_FILES['ufile2']['name'][0])
			AND ($filesize2!=0))
			{
			if('jpg'== strtolower(substr($HTTP_POST_FILES['ufile2']['name'][0], strrpos($HTTP_POST_FILES['ufile2']['name'][0], '.') + 1)))
	
					{
					$query =(mysql_query( "SELECT id FROM virtual_tour WHERE id='$last_id'"));
					if (mysql_num_rows($query) < 1) 
									{	
									if($_POST['link']==''){$link_tur="http://google.ro";}
									else{$link_tur=$_POST['link'];}
									
									//copy file to where you want to store file
									copy($HTTP_POST_FILES['ufile2']['tmp_name'][0], $path2);
									insertUploadTur($last_id,$path2,$link_tur);
									$error_tur="Image <strong> ".$HTTP_POST_FILES['ufile2']['name'][0]." </strong> was loaded successfully <br/>";
									
									}
					else{
						while($row = mysql_fetch_array($query))
						{	
							if($row['id']!=$last_id)
							{  
								if($_POST['link']==''){$link_tur="http://google.ro";}
								else{$link_tur=$_POST['link'];}
									
								//copy file to where you want to store file
								copy($HTTP_POST_FILES['ufile2']['tmp_name'][0], $path2);
								insertUploadTur($last_id,$path2,$link_tur);
								$error_tur="Image <strong> ".$HTTP_POST_FILES['ufile2']['name'][0]." </strong> was loaded successfully <br/>";
									
									
							}
							else{$error_tur="Image exist.<br/>";break;}
						}
					}
				
					}
			else {$error_tur="Only .jpg extension";}
			}
else {$error_tur="Image with name <strong>".$HTTP_POST_FILES['ufile2']['name'][0]." </strong>exist on server. <br/>";}  
}

}

///////////////////////////////////////////////////////
echo’

Upload file #tooltip{ position:absolute; border:1px solid #333; background:#f7f5d1; padding:2px 5px; color:#333; display:none; } table.uploads { font-family:Arial, sans-serif; color:#323232; font-size:13px; } table.uploads table { border:1px solid silver; background:#f7f7f7; } th { font-size:15px; background:#305C8B; color:#fff; text-align:left; padding:0px 10px; margin:10px 0px; } th strong { margin-top:10px; line-height:2em; } table tr td { padding-bottom:10px; } input.upload_image { background:#305C8B; color:#fff; width:80px height:30px; padding:2px 6px; border:1px solid #263D5E; } div.error_image { text-align:center; margin:0 auto; font-family:Arial, sans-serif; font-size:15px; font-weight:normal; padding:10px; margin:10px 0px; color:#323232; background:#b8e287; } th.upload_complete { padding:6px; font-family:Arial, sans-serif; font-size:14px; background:#436f9f; } th.pause_rows { background:none; padding:4px; } .table_with_images { font-family:arial, sans-serif; font-size:13px; color:#323232; background:#c7eb9e; width:100%; } .table_with_images tr td strong { font-family:arial, sans-serif; font-size:13px; color:#263D5E; font-weight:bold; padding:5px; } .table_with_images tr td { padding-bottom:0px; padding-top:0px; } tr td.name_image { font-size:13px; font-weight:bold; color:#a60000; padding:10px 2px 5px; } a.tooltip { cursor:pointer; } .ad_success { width:400px; margin:0 auto; text-align:center; } .ad_success ul { list-style-type:none; } .ad_success ul li { line-height:1em; padding:0px; } .ad_success ul li a { color:#444; font-family:arial, sans-serif; font-size:13px; font-weight:bold; text-decoration:none; } .ad_success ul li span { position:relative; top:-11px; padding-left:5px; } .ad_success ul li a:hover { color:#777; } div#options_right { width:400px; margin:0 auto; } ';

echo’

'; include 'header.php'; echo'
'; include 'bar.php'; echo'
'; echo '
';
	echo $error_image;echo'</br>';
	echo $error_tur; echo'</div>';

								echo'<table class="uploadeuri" width="600" border="0" align="center" cellpadding="0" cellspacing="1">
											<tr>
												<form action="images.php?id='.$last_id.'" method="post" enctype="multipart/form-data" name="form1" id="form1">
												<input type="hidden" name="action" value="load" /></td> 
												<input type="hidden" name="v_position" value="bottom" /></td> 
												<input type="hidden" name="h_position" value="right" /></td> 
												<input type="hidden" name="wm_size" value=".15" /></td> 
												<input type="hidden" name="watermark" value="../admin/images/watermark.png" /></td> 
											<td>
										<table width="100%" align="left" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
											<tr>
												<th colspan="2"><strong>Upload pentru imagini</strong></td>
											</tr>
											<tr>
												<td><strong>Description image</strong> </td>
												<td><input size="50" type="text" name="description_image" value="" /></td> 	
											</tr><tr>
												<td><strong>Select image</strong></td>
												<td><input name="ufile1[]" type="file" size="50" /></td>
											</tr>
											<tr>
												<th colspan="2">
													<strong>Upload for virtual tour</strong>
												</th>
											</tr>
											<tr>
												<td><strong>Image virtual tour</strong></td>
												<td><input name="ufile2[]" type="file" size="50" /></td>
											</tr>
											<tr>
												<td><strong>Link virtual tour</strong> </td>
												<td><input size="50" type="text" name="link" value=" http://mylink/admin/upload_photos/'.$last_id.'/virtual_tour/" /></td> 	
											</tr>
											<tr>
											<td></td>
											<td align="left"><input class="upload_image" type="submit" name="Submit" value="Upload" /></td>
											</tr>
											</table>
											</td>
											</form>
											</tr>
						</table>
						<form action="images.php?action=modif" method="post" enctype="multipart/form-data" >
						<table class="table_with_images" width="800" border="0"  cellpadding="0" cellspacing="1" align="center">';
					//	if(isset($_POST['action']) AND $_POST['action']=='load'){
						if(file_exists("../admin/upload_photos/$last_id/images"))
						{
						echo'<tr><th class="upload_complete" colspan="6">Images was uploaded</th></tr>';
						if ($handle = opendir("../admin/upload_photos/$last_id/images")) {
							while (false !== ($file = readdir($handle))) {
   
							if ($file != "." && $file != "..") {	   
												echo "
												<tr>
														<td class='name_image' >$file</td>
														</tr>
														<tr>"; $modif_row=selectImage($last_id,$file);
																			echo'
																			     <td><a href="'.$modif_row['link_image'].'" title="Preview"><img src="'.$modif_row['link_thumb'].'"  ></a></td>
																				 <td><a class="tooltip" title="Primary photo" ><div class="menu_pic55" id_photo="'.$modif_row['id_photo'].'" id="'.$modif_row['id2'].'" file="'.$file.'" type="images" description="'.$modif_row['description_image'].'">';
																					if($modif_row['first_photo']==1){echo'<img src="images/primary_photo.jpg" alt="Set primary photo"></div></a>	</td>';}
																												else{echo'<img src="images/priamry_photo_activa.jpg" alt="Set primary photo"></div></a>	</td>';}
																				echo'								
																				 <td><strong>Description</strong> <input size="40" id="in'.$modif_row['id_photo'].'" type="text" name="description" value="'.$modif_row['description_image'].'" /></td>
																				
																				<td><a class="tooltip" title="Apply desc modify"><div class="menu_pic44" id_photo="'.$modif_row['id_photo'].'" id="'.$modif_row['id2'].'" file="'.$file.'" type="images" description="'.$modif_row['description_image'].'"><img src="images/modify_description2.jpg" alt="Preview image"></div></a>	</td>
																				<td><a class="tooltip" title="Preview image" href="'.$modif_row['link_image'].'"><img src="images/preview_photo.jpg" alt="Preview image"></a></td>
																				<td><a class="tooltip" title="Delete image"><div class="menu_pic22" id="'.$modif_row['id2'].'" file="'.$file.'" type="images"><img src="images/delete_photo.jpg" alt="Delete image"></div></a> </td>
																			';
																			echo"</td></tr>	
														<tr>
														<th class='pause_rows' colspan='6'><hr/></th>";	
													echo'</tr>';
													}
												}
											closedir($handle);
											}
											echo'
											</table>';
						echo'
						
						<table class="table_with_images" width="800" border="0"  cellpadding="0" cellspacing="1" align="center">
						<tr><th class="upload_complete" colspan="6">Preview virtual tour</th></tr>';
						if ($handle = opendir("./upload_photos/$last_id/virtual_tour")) {
							while (false !== ($file2 = readdir($handle))) {
   
							if ($file2!= "." && $file2 != "..") {	   
												echo "<tr>
														<td class='name_image'>$file2</td>
														<td>"; $modif_row2=selectTur($last_id,$file2);
																			echo' 
														</tr><tr>          <td><a href="'.$modif_row2['photo_tour'].'" title="Preview"><img src="'.$modif_row2['photo_tour'].'"  height="80px" width="310px"></a></td>
																			<td><strong>Link tur virtual</strong> <input size="40" id="tur'.$modif_row2['id'].'" type="text" name="description" value="'.$modif_row2['link_tur'].'" /></td> 
																			<td><a class="tooltip" title="Modify link" ><div class="menu_pic44" id="'.$modif_row2['id'].'" file="'.$file2.'" type="virtual_tour"><img src="images/modify_description2.jpg" alt="Modify link"></div></a>	</td>
																			<td><a class="tooltip" title="Preview image" href="'.$modif_row2['photo_tour'].'"><img src="images/preview_photo.jpg" alt="Preview image tour"></a></td>
																			<td><a class="tooltip" title="Delete virtual tour" ><div class="menu_pic22" id="'.$modif_row2['id'].'" file="'.$file2.'" type="virtual_tour"><img src="images/delete_photo.jpg" alt="Delete image"></div></a> </td>
																				
																			
																	
														<td><hr/></td>	
														</tr>';
													}
												}
											closedir($handle);
											}					
											
						}
				echo	'
				<th class="pause_rows" colspan="6"><hr/></th>	
				</table>
				</form>';
	$the_result = mysql_query(selectID($last_id)) or die("Eroare: ".mysql_error());
	if($rowe = mysql_fetch_array($the_result)){
	$id=$rowe['id'];
	
	
	
	
	echo "
	
	<div class='ad_success'>
	<ul>
		<li><a href='index.php'><img src='images/back_to_admin.jpg' alt='Click for go to admin page' /><span>Click for go to admin page</span></a></li>
		<li>
	
	
	<a target=_blank'' href='";
	 if ($rowe['category']=="Apartament") {
								echo "../desc-apartament.php?id=".$rowe['id'];
							}
							echo "' title='Go to ads page'><img src='images/info.jpg' alt='Info ads'/><span>Go to ads page</span></a>";
							}
					echo'		
	
</li>
</ul>
	</div>
 </div>
 </div>

<div id="footer">';
	 include 'footer.php'; echo'
</div>
';
	}[/embed]

First I will probably come up sounding harsh, but this isn’t my attention. Don’t post all your code, just post the segment of code you think is giving you the problem(s).

Something like this (Just an example):

[php] if ($fileNumber < 10) {
$this->uniqueFileName = $name . ‘0’ . $fileNumber;
} else {
$this->uniqueFileName = $name . $fileNumber;
}

    $this->imageName = $imgFile['name'];
    $this->imageType = $imgFile['type'];
    // Get the image extension:
    $this->dirName = pathinfo($imgFile['name'], PATHINFO_DIRNAME);
    $this->imageExt = pathinfo($imgFile['name'], PATHINFO_EXTENSION);
    $this->imageError = $imgFile['error'];
    $this->imageSize = $imgFile['size'];
    $this->imageTemp = $imgFile['tmp_name'];

    if ($this->imageSize > 800000) {
        return $this->imageError = "Image is greater than 800K";
    }

    if (!in_array($this->imageExt, self::$allowedExts)) {
        return $this->imageError = "File isn't an image.";
    }

    if (!in_array($this->imageType, self::$allowedTypes)) {
        return $this->imageError = "Unknown Image Type!";
    }[/php]  

Encase in [php][php] tags and give what kind of error(s) you are getting.

Second just looking over you code I sense you don’t have a grasp of how to upload images written in php, just do a Google search on php upload images tutorials should help you in this regard. I would help you more in this department, but I threw in OOP in with my image scripts and that would only confuse you even more. 8)

[php]

<?php require_once('includes/config.php'); include 'db/connect.php'; include 'db/functions.php'; if(!isset($_SESSION['login'])) $_SESSION['login'] = 'No'; if($_SESSION['login'] != 'Yes') { echo '';} else { if(isset($_GET['id'])){$last_id=$_GET['id'];} $error_image=''; $error_tur=''; if(isset($_POST['action']) AND $_POST['action']=='load'){ $directory="./upload_photos/$last_id"; $filesize1=$HTTP_POST_FILES['ufile1']['size'][0]; $filesize2=$HTTP_POST_FILES['ufile2']['size'][0]; $path1= "../admin/upload_photos/$last_id/images/".$HTTP_POST_FILES['ufile1']['name'][0]; $path2= "../admin/upload_photos/$last_id/virtual_tour/".$HTTP_POST_FILES['ufile2']['name'][0]; $path3= "../admin/upload_photos/$last_id/thumbnails/".$HTTP_POST_FILES['ufile1']['name'][0]; if(!file_exists($directory)) { mkdir("../admin/upload_photos/$last_id"); chmod("../admin/upload_photos/$last_id", 0777); mkdir("../admin/upload_photos/$last_id/images"); chmod("../admin/upload_photos/$last_id/images", 0777); mkdir("../admin/upload_photos/$last_id/thumbnails"); chmod("../admin/upload_photos/$last_id/thumbnails", 0777); mkdir("../admin/upload_photos/$last_id/virtual_tour"); chmod("../admin/upload_photos/$last_id/virtual_tour", 0777); } if(isset($HTTP_POST_FILES['ufile1']['name'][0]) AND ($HTTP_POST_FILES['ufile1']['name'][0]!='')) { if (!file_exists("../admin/upload_photos/$last_id/images/".$HTTP_POST_FILES['ufile1']['name'][0]) AND ($filesize1!=0)) { if('jpg'== strtolower(substr($HTTP_POST_FILES['ufile1']['name'][0], strrpos($HTTP_POST_FILES['ufile1']['name'][0], '.') + 1))) { $query0 =(mysql_query( "SELECT * FROM imagini WHERE id2='$last_id'")); if (mysql_num_rows($query0) < 1) { //copy file to where you want to store file include 'watermark.php'; include 'thumbnails.php'; ///////////////////////////////////// //copy($HTTP_POST_FILES['ufile1']['tmp_name'][0], $path1); insertUploadImage($last_id,$path1,$path3,1); $error_image="Image ".$HTTP_POST_FILES['ufile1']['name'][0]." was loaded successfully
"; } else{ include 'watermark.php'; include 'thumbnails.php'; //copy($HTTP_POST_FILES['ufile1']['tmp_name'][0], $path1); insertUploadImage($last_id,$path1,$path3,0); $error_image="Image ".$HTTP_POST_FILES['ufile1']['name'][0]." was loaded successfully
"; } } else {$error_image="Only .jpg extension
";} } else {$error_image="Image with name ".$HTTP_POST_FILES['ufile1']['name'][0]." exist on server
";} } if(!file_exists($directory)) { mkdir("../admin/upload_photos/$last_id"); chmod("../admin/upload_photos/$last_id", 0777); mkdir("../admin/upload_photos/$last_id/images"); chmod("../admin/upload_photos/$last_id/images", 0777); mkdir("../admin/upload_photos/$last_id/thumbnails"); chmod("../admin/upload_photos/$last_id/thumbnails", 0777); mkdir("../admin/upload_photos/$last_id/virtual_tour"); chmod("../admin/upload_photos/$last_id/virtual_tour", 0777); } if(isset($HTTP_POST_FILES['ufile2']['name'][0]) AND ($HTTP_POST_FILES['ufile2']['name'][0]!='') ) {if (!file_exists("../admin/upload_photos/$last_id/virtual_tour/".$HTTP_POST_FILES['ufile2']['name'][0]) AND ($filesize2!=0)) { if('jpg'== strtolower(substr($HTTP_POST_FILES['ufile2']['name'][0], strrpos($HTTP_POST_FILES['ufile2']['name'][0], '.') + 1))) { $query =(mysql_query( "SELECT id FROM virtual_tour WHERE id='$last_id'")); if (mysql_num_rows($query) < 1) { if($_POST['link']==''){$link_tur="http://google.ro";} else{$link_tur=$_POST['link'];} //copy file to where you want to store file copy($HTTP_POST_FILES['ufile2']['tmp_name'][0], $path2); insertUploadTur($last_id,$path2,$link_tur); $error_tur="Image ".$HTTP_POST_FILES['ufile2']['name'][0]." was loaded successfully
"; } else{ while($row = mysql_fetch_array($query)) { if($row['id']!=$last_id) { if($_POST['link']==''){$link_tur="http://google.ro";} else{$link_tur=$_POST['link'];} //copy file to where you want to store file copy($HTTP_POST_FILES['ufile2']['tmp_name'][0], $path2); insertUploadTur($last_id,$path2,$link_tur); $error_tur="Image ".$HTTP_POST_FILES['ufile2']['name'][0]." was loaded successfully
"; } else{$error_tur="Image exist.
";break;} } } } else {$error_tur="Only .jpg extension";} } else {$error_tur="Image with name ".$HTTP_POST_FILES['ufile2']['name'][0]." exist on server.
";} } } ?>[/php]

I don’t know php enough, so I need your help, sorry for my bad english. I need this script for upload image & write in database. please :frowning: help…

Sponsor our Newsletter | Privacy Policy | Terms of Service