2013-07-24 47 views
0

我正在编写一个应用程序,用户将从该应用程序上传服务器上的文件。我从互联网上找到了一个php脚本,但我不知道如何告诉脚本在哪里上传数据。这可能是一个愚蠢的问题,但我不是PHP程序员。我在我的java代码中使用这个php脚本。上传服务器上的文件

这是脚本。

<?php 
    $filename="abc.xyz"; 
    $fileData=file_get_contents('php://input'); 
    echo("Done uploading"); 
?> 

问候

+0

请参阅PHP手册上的$ _FILE文档,这里有很多示例。 – Virus721

+0

你是什么意思,你在你的java代码中使用php脚本?你究竟想在这里完成什么,你是否试图从用户的浏览器上传文件到服务器? –

回答

0

文件名实际上是文件路径以及新文件的名称,在那里设置路径并创建具有写入权限的文件。

确保您为服务器提供完整路径而不是相对路径,并且您具有在其中创建文件所需的权限。

+0

嗨,兄弟,如何更改文件的下载链接,我的意思是此刻下载链接与上传链接相同,这意味着任何人都可以进入我的服务器。那么有没有办法改变下载地址。 – Naruto

+0

您可以制作一个php脚本,从其他链接提取上传文件,并上传文件,然后将文件移动到其他位置。 – Krotz

1

这是上传文件的一个可怕的方式,你好得多使用形式和$_FILES超全局。

看看W3Schools PHP File Upload Tutorial;请阅读全部内容。为了进一步阅读,请查看文件上传中的PHP Manual页面。

file输入类型将创建HTML表单的上传框:

<form action="upload_file.php" method="post" 
enctype="multipart/form-data"> 
    <label for="file">Filename:</label> 
    <input type="file" name="file" id="file"><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

错误检查和确认后,该文件是你期待什么(很重要:让用户什么上传到你的服务器是一个巨大的安全风险),你可以将上传的文件移动到你的PHP服务器上的最终目的地。

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/abc.xyz"); 
0

请务必参阅PHP Manual

这里有一个基本的例子,让你开始:

HTML:

<html> 
<body> 
    <form enctype="multipart/form-data" action="upload.php" method="post"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 
    Choose a file to upload: <input name="uploaded_file" type="file" /> 
    <input type="submit" value="Upload" /> 
    </form> 
</body> 
</html> 

PHP:

<?php 
//Сheck that we have a file 
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { 
    //Check if the file is JPEG image and it's size is less than 350Kb 
    $filename = basename($_FILES['uploaded_file']['name']); 
    $ext = substr($filename, strrpos($filename, '.') + 1); 
    if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 350000)) { 
    //Determine the path to which we want to save this file 
     $newname = dirname(__FILE__).'/upload/'.$filename; 
     //Check if the file with the same name is already exists on the server 
     if (!file_exists($newname)) { 
     //Attempt to move the uploaded file to it's new place 
     if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { 
      echo "It's done! The file has been saved as: ".$newname; 
     } else { 
      echo "Error: A problem occurred during file upload!"; 
     } 
     } else { 
     echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; 
     } 
    } else { 
    echo "Error: Only .jpg images under 350Kb are accepted for upload"; 
    } 
} else { 
echo "Error: No file uploaded"; 
} 
?> 

有关更多信息,请参阅documention

希望这会有所帮助!