简体   繁体   English

我无法将文件上传到php中的本地主机文件夹中

[英]I am unable to upload file to my local host folder in php

I have the follwing code 我有以下代码

<form enctype="multipart/form-data" action="upload.php" method="POST">
 Please choose a file: <input name="uploaded" type="file" /><br />
 <input type="submit" value="Upload" />
 </form>  

<?php 
 $target = "upload/"; 
 $target = $target . basename( $_FILES['uploaded']['name']) ; 
 $ok=1; 
 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 {
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
 } 
 else {
 echo "Sorry, there was a problem uploading your file.";
 }
 ?> 

my page is in http://local.host/nausal/upload.php 我的页面在http://local.host/nausal/upload.php

now I am having the follwing error though i have created a folder in site with name upload. 现在,虽然我在站点中使用名称上传创建了一个文件夹,但出现了以下错误。

Notice: Undefined index: uploaded in C:\\wamp\\www\\Nausal\\upload.php on line 15 注意:未定义的索引:在第15行的C:\\ wamp \\ www \\ Nausal \\ upload.php中上传

Notice: Undefined index: uploaded in C:\\wamp\\www\\Nausal\\upload.php on line 17 Sorry, there was a problem uploading your file. 注意:未定义的索引:在第17行的C:\\ wamp \\ www \\ Nausal \\ upload.php中上传抱歉,上传文件时出现问题。

If the form is a part of upload.php too, you need to encapsulate the PHP-code and first check if $_FILES is not empty, otherwise you will always get a Notice if you only want to display the form. 如果表单也是upload.php的一部分,则需要封装PHP代码,并首先检查$ _FILES是否不为空,否则,如果只想显示表单,则始终会收到一条Notice。

<?php
  if(!empty($_FILES))
  {
        //your PHP-code here
  }
?>
<!-- your form here -->
  • Khan sb Place this code in file called upload.php Khan sb将此代码放置在名为upload.php的文件中
  • create a folder called upload where you created upload.php 创建一个名为upload的文件夹,在其中创建upload.php

  • change echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 更改echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; to echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded"; echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded"; echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";

This will upload file successfully. 这将成功上传文件。 First time when upload.php is uploaded there is no file selected so you will see some errors. 首次上传upload.php时,没有选择文件,因此您会看到一些错误。 But after you select a file and click upload you will not see any error. 但是,在选择文件并单击上载后,您将不会看到任何错误。 To avoid this error do as suggested by Dr. Molle 为了避免此错误,请按照Molle博士的建议进行操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM