登录 | 注册 | FAQ
Anonymous

how to uplaod image in php script?

+ 发表回复

6 篇帖子 分页: 1 / 1


how to uplaod image in php script?

freelancerboy » 周日 4月 05, 2015 4:58 pm

I am new in php and learning regularly.
how to upload image at php script? i want to upload image randomly.
anybody can explain me easily?
<input type="file" name="upload" />
头像

freelancerboy

  • 帖子: 3
  • 注册: 周三 4月 01, 2015 3:33 pm
  • 地址: Bangladesh

Re: how to uplaod image in php script?

XainPro » 周三 4月 15, 2015 5:50 pm

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
头像

XainPro

  • 帖子: 3933
  • 注册: 周五 2月 17, 2012 8:10 pm

Re: how to uplaod image in php script?

Chikwado » 周五 4月 17, 2015 10:05 am

This code does not work for me, I have this code here: http://chiquado.site90.net/upload It return error, was it from my hosting server, or what?
头像

Chikwado

  • 帖子: 87
  • 注册: 周一 7月 29, 2013 11:36 pm
  • 地址: Nigeria
--
Focus On Advance Science And Programming.

Re: how to uplaod image in php script?

XainPro » 周五 4月 17, 2015 1:53 pm

it's not showing the error what is the error exactly ?
头像

XainPro

  • 帖子: 3933
  • 注册: 周五 2月 17, 2012 8:10 pm

Re: how to uplaod image in php script?

Chikwado » 周日 4月 19, 2015 4:14 pm

@Xainpro help me check this script once more, This script always return error http://chiquado.site90.net/upload
头像

Chikwado

  • 帖子: 87
  • 注册: 周一 7月 29, 2013 11:36 pm
  • 地址: Nigeria
--
Focus On Advance Science And Programming.

Re: how to uplaod image in php script?

XainPro » 周六 5月 02, 2015 3:33 pm

the link is broken
头像

XainPro

  • 帖子: 3933
  • 注册: 周五 2月 17, 2012 8:10 pm


+ 发表回复

分页: 1 / 1