<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代码来创建一个简单的上传文件表单。这段代码用于从同一台计算机上传文件到本地主机,但是我如何将其更改为从Google Images上传?
这有点粗糙,但它可以工作。
在文件选择页面中添加另一个块
<!-- 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. */