-2
<html><head><title>Uploading Pictures</title></head>
<body bgcolor="lavender">
<h3>Uploading Files</h3>
<form
enctype="multipart/form-data"
action="upload_move_file.php"
method="post">
Browse and select the picture you want to upload: <br />
<input name="picture_file" type="file" />
<br />
<input type=submit value="Get File"/>
</form>
</body>
</html>
 (The PHP Script)
 <html><head><title>File Uploads</title></head>
 <body bgcolor="#33ff33">
 <font face="verdana" size="+1">
 <?php

 echo "The uploaded file is: ", $_FILES['picture_file']
 ['tmp_name'], "<br />";
 $filename=$_FILES['picture_file']['name'];
 $filesize=$_FILES['picture_file']['size'];
 $directory='C:\wamp\www\examples\\';
 $uploadFile = $directory . $filename;
 echo "The moved file is: $uploadFile<br />";
 if (move_uploaded_file($_FILES['picture_file']['tmp_name'],$uploadFile)){
 echo "The file is valid and was successfully uploaded.<br /> ";
 echo "The image file, $filename, is $filesize bytes.<br/>";
}
 ?>
<center>
 <br />
<img src=<?php echo "\"\examples\\$filename\"";?>
width="250" height="175" border="3">
</center>
</font>
</body>
</html>

我编写了 html 和 php 代码来创建一个简单的上传文件表单。此代码用于将文件从同一台计算机上传到 localhost,但是如何将其更改为从 Google 图片上传?

4

1 回答 1

0

这有点粗略,但它有效。

在文件选择页面中添加另一个块。

<!-- use a url to get the file -->
<form action="upload_move_file.php" method="post">
<input type="text" name="url">
<input type="submit" value="Get file from url"/>
</form>

然后在您的 upload_move_file.php 中放置一个适用于 url 而不是系统上传的部分。

/* make a folder to wite the downloads to and make it accessible 
** write permission for the web user */
$path = "files";
$uploaddir = $path.'/' ;

/* if your form sent the url, use something like this */
if (isset($_POST['url'])){
    $url_string = $_POST['url'];

    /* you will need the name from the url and image type */
    $name = substr($url_string , strrpos($url_string ,"/") + 1 ) ;
    $extension = substr($name , strrpos($name ,".") + 1 ) ;

    /* download and store the image in a temp file */
    $img = file_get_contents($url_string);

    /* Write the file over to the new name */
    $uploadfile = $path ."/". $name;
    file_put_contents($uploadfile, $img);

    /* you will need the file size */
    $img_size = filesize($path ."/". $name);    

    /* You could try loading that stuff into FILES array to 
    ** trick the upload script.

    $_FILES['picture_file']['name'] = $name;
    $_FILES['picture_file']['type'] = "image/$extension";
    $_FILES['picture_file']['size'] = $img_size;
    $_FILES['picture_file']['tmp_name'] = $uploadfile;
    $_FILES['picture_file']['error'] = '';

    OR just show it */

    die('Success!
      <center>
        <br />
        <img src="'.$uploadfile.'" 
        width="250" height="175" border="3" />
    </center>');


} /* else use the other idea that handled the local image. */
于 2013-04-19T00:05:08.237 回答