问题
I'm trying to create a website were pictures can be uploaded and saved into a folder in the server. However, the code does not go past the second if. Is there something I'm missing? Thanks in advance.
The PHP:
if (isset($_FILES["file"]["name"])){
if($_FILES['image']['error']==0){
$file = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
$newname = $erabiltzaile.".png";
$target = 'irudiak/profile/users/'.$newname;
move_uploaded_file( $_FILES['image']['name'], $target);
}
}else{
$image=null;
$image_name="";
}
The HTML:
<div id='igoera'>
<input name='image' id ='image' type='file' accept='image/png' onchange='loadFile(event)' /><br /><br />
<img width='300' id='preview'/><br>
</div>
回答1:
Looks like you've missed input file name
in your condition:
if (isset($_FILES["image"]["name"])){
回答2:
In the manual there's a section on file uploads http://php.net/manual/en/features.file-upload.php which shows the values for the error variable in the $_FILES
array.
A successful upload should result in the error value being UPLOAD_ERR_OK
.
So change the second if
condition to:
if($_FILES['image']['error']=='UPLOAD_ERR_OK'){
来源:https://stackoverflow.com/questions/34590212/php-image-upload-problems