I wrote a script to load a picture into the database (in the database the path, the picture in the directory). Everything is well prescribed only the picture in the directory does not appear. Maybe I'm doing it wrong? I give a piece of code responsible for loading ....

if ($erro!=true){ $file=$_POST['myfile']['name']; $dir='http://localhost/bol/img/'; $up_file=$dir.basename($file); echo ($up_file); if (is_uploaded_file($_FILES['myfile']['tmp_name'])) { $n=mt_rand(0,997); $file_path=$dir.$n.jpeg; if (!file_exists($file_path)) { $_FILES['myfile']['name']=$n.'.'.jpeg; move_uploaded_file($_FILES['myfile']['tmp_name'],$dir.$_FILES['myfile']['name']); $put=$dir.$_FILES['myfile']['name']; } dbConnect(); mysql_query("INSERT INTO image (put, nazv,opis,kto_kuda) VALUES ('{$put}','{$nazv}','{$opis}','{$kto_kuda}')") or die (mysql_error()); }} 

Loading takes place through the form ( <form method="post" enctype="multipart/form-data"> )

  • Do you want to upload pictures to the database? - Farhod
  • It is written the same - in the database only the path is specified and the picture itself lies in the directory. Catalog created. img. - new_russian_man

2 answers 2

 $dir='http://localhost/bol/img/'; 

Why do you have the full path with the protocol? Try to change to

 $dir='./bol/img/'; 

    Idaho37 is right. You specify the wrong path. Here is a working example.

     include("core.php"); $panels = new panel; echo '<a href="javascript:self.close()">закрыть окно</a> '; if($panels->checkauth()) { echo '<html> <body> <FORM ENCTYPE="multipart/form-data" ACTION="" METHOD=POST> <h4>Разрешено загружать только фотографии<br />Выберете файл не (больше 1мб):</h4> <table><tr><td>Задать имя фотографии:</td><td> <input type="text" size="32" name="img_name" /></td></tr><tr><td> Выберить:</td><td> <INPUT NAME="myfile" TYPE="file"></td></tr><tr><td colspan=2 align=right> <INPUT TYPE="submit" VALUE="Загрузить"></td></tr></table> </FORM> </body> </html>'; if(isset($_FILES["myfile"])) { //Настройки Upload-а $maxsize = 1024*1024*1; //Больше 1мб не может $valid_types = array("image/png", "image/jpg", "image/gif", "image/jpeg"); $dir="content_images/"; //Обычные переменные $myfile = $_FILES["myfile"]["tmp_name"]; $myfile_name = $dir.time()."_".$_FILES["myfile"]["name"]; $myfile_size = $_FILES["myfile"]["size"]; $myfile_type = $_FILES["myfile"]["type"]; $error_flag = $_FILES["myfile"]["error"]; if(strlen($_POST['img_name'])==0) $img_nm="No Name"; else $img_nm=$_POST['img_name']; if($error_flag == 0) { if($maxsize>$myfile_size) { // if(in_array($myfile_type, $valid_types)) // { if(move_uploaded_file($_FILES['myfile']['tmp_name'], $myfile_name)) { $image=new Imagick(); $image->readImage($myfile_name); include("mysql_class.php"); $mysql = new db; if($mysql->query_insert("INSERT INTO pr_image (u_id, image_url, image_name, adddate) VALUES('".$_SESSION['id']."', '".$myfile_name."', '".$img_nm."','".time()."')")) { print("Ссылка на файл: ".$myfile_name." <br />"); print("MIME-тип файла: ".$myfile_type."<br>"); print("Размер файла: ".(round($myfile_size/1024))." kb <br><br>"); echo '<br /><img width=500px src="'.$myfile_name.'"'; } else { unlink($myfile_name); echo "<h2>Ошибка Базы Данных</h2>"; } } else { unlink($myfile); echo "<h2>Ошибка Сервера</h2>"; } // } // else // { // unlink($myfile); // echo "<h2>Это не Фотография</h2>"; // } } else { unlink($myfile); echo "<h2>Файл привышает 1мб!</h2>"; } } } } else { echo "<font color=red>Пожалуйста Авторизируйтесь</font>"; }