如何在php上的服务器上传文件?

时间:2013-12-14 22:13:50

标签: php

这是我的表格:

<form action="insc.php" method="post" name="inregistrarec">
  <div class="reg_section personal_info">
    <h3>Informatii generale despre companie</h3>
    <input name="Denumirea" type="text"  placeholder="Denumirea" style="width:440px;" >
    <input name="Domeniul"  type="text"  placeholder="Domeniul" style="width:440px;">
    <input name="Adresa"     type="text"  placeholder="Adresa" style="width:440px;">
    <input name="Tel"        type="text"  placeholder="Telefonul" style="width:440px;">
    <input name="Email"      type="text"  placeholder="E-mail" style="width:440px;">
    <input name="Fax"        type="text"  placeholder="Fax" style="width:440px;">
    <input name="file"       type="file"  placeholder="Foto" style="width:440px;">
  </div>
  <div class="reg_section password">
    <input name="Login"      type="text"     placeholder="Login" style="width:440px;">
    <input name="password"  type="password" placeholder="Parola" style="width:440px;">
    <input name="password1" type="password" placeholder="Confirma parola"style="width:440px;">

  </div>
  <div class="reg_section password">
    <h3>Despre companie</h3>
    <textarea name="textarea" id="textarea"        placeholder="Descriere" style="width:440px;"></textarea>
    <input    name="url"      type="url"  id="url" placeholder="Pagina companiei" style="width:440px;">
  </div>
  </p>
  <p class="submit">
    <input type="submit" name="submit" value="Înregistrare"></p>
</form>

这是我连接的insc.php文件:

    <?php

    $denumirea = mysql_real_escape_string($_POST['Denumirea']);
    $domeniul = mysql_real_escape_string($_POST['Domeniul']);
    $adresa = mysql_real_escape_string($_POST['Adresa']);
    $tel = mysql_real_escape_string($_POST['Tel']);
    $email =mysql_real_escape_string( $_POST['Email']);
    $fax = mysql_real_escape_string($_POST['Fax']);
    $login = mysql_real_escape_string($_POST['Login']);
    $file=mysql_real_escape_string($_POST['file']);
    $password = mysql_real_escape_string($_POST['password']);
    $password1 = mysql_real_escape_string($_POST['password1']);
    $descriere= mysql_real_escape_string($_POST['textarea']);
    $url= mysql_real_escape_string($_POST['url']);

$id = mysql_connect("localhost", "root", "");
    $db = mysql_select_db("site", $id);



if(isset($_POST['submit'])){



    //This is the directory where images will be saved 
 $target = "imagini/"; 
 $target = $target . basename( $_FILES['file']['name']); 

 //This gets all the other information from the form 
 $file=($_FILES['file']['name']); 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file.";
 } 
$target = mysql_real_escape_string($target);
    $dom=mysql_query("insert into domeniu (Domeniu) values ('$domeniul')")  or die(mysql_error());
    $id_dom=mysql_query("SELECT * FROM `domeniu` order by Id_Domeniu desc limit 1;")  or die(mysql_error()); 

    $numrows=mysql_num_rows($id_dom);

    if($numrows!=0)
    {
        while($row=mysql_fetch_assoc($id_dom))
        {
            $domeniu_id=$row['Id_Domeniu'];
            $domeniu=$row['Domeniu'];

            }
    }
    $res=mysql_query("INSERT INTO companie (Den_Comp,Id_Domeniu_FK,Adresa,Tel,Email,Fax,Foto,Login,Parola,Descriere,Pag_Comp,Foto) VALUES ('$denumirea','$domeniu_id', '$adresa','$tel','$email','$fax','$file','$login','$password','$descriere','$url',$target)") or die(mysql_error("eroare"));
header('location:index(2).php');
                    }

        else echo"eroare";
   ?>

但我有错误:`未定义的索引:C:\ wamp \ www \ insc.php中的文件。 我尝试过其他方式,但没有任何帮助。 我该怎么做才能使用这段代码?

1 个答案:

答案 0 :(得分:1)

您必须定义

enctype="multipart/form-data"

在您的<form> - 标记中实际将文件上传到服务器。