简体   繁体   中英

php move_uploaded_file() doesn't work in production

So this is my problem: Im hosted on tsrato and want to upload a file to my Webserver, I was too lazy to create my own, because this problem is haunting me for days now. The Webserver has got a tmp Directory I think and I don't have the accessebility to redirect ther tmp directory to my uploads folder. So how can I send the file dirextly to htdocs and in my uploads folder. the php Code below is in a other directory called inc. The size of the file doesnt matter and the security I just want to get one file moved to my directory. This is the directory to my Webspace /mnt/rid/XX/X2/51XX52582/htdocs/

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["userfile"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["userfile"]["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["userfile"]["size"] > 500000000) {
  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["userfile"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["userfile"]["name"]). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}

Check to see if you /uploads folder is writable. If its not writable and you don't have the permissions the files will not be transferred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM