So habe ich ein PHP-Skript, wo ich ein Bild von einem HTML-Formular hochladen. Die PHP-Datei sieht wie folgt aus:Nicht in der Lage, Bilder mit PHP korrekt zu laden
<?php
function generateRandomString($length = 10) {
$characters =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$herman_is_the_best;
if(isset($_POST["place"]) && isset($_POST["submit"])) {
$herman_is_the_best = $_POST["place"];
$target_dir = "../places/" . $herman_is_the_best . "/" ;
$realtarget = $target_dir . generateRandomString() . "." .
pathinfo($target_file,PATHINFO_EXTENSION);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
//check if there is a folder for the location alredy
$direction = "../places/" . $herman_is_the_best . "/";
if(!is_dir($direction)){
//make direction for place
mkdir($direction);
// url encode the address
$address = urlencode($herman_is_the_best);
// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=". $address ."&key=AIzaSyDeqk1Oc6TkKQkQph_-P_4U9jTLLJv0G98";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// response status will be 'OK', if able to geocode given address
if($resp['status']=='OK'){
// get the important data
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
// verify if data is complete
if($lati && $longi){
// put the data in the array
$data_arr = array();
array_push(
$data_arr,
$lati,
$longi
);
}
}
$my_file = '../places/' . $herman_is_the_best .'/cordinates.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates file
$long = $data_arr[0];
$lati = $data_arr[1];
fwrite($handle, $long . "\r\n"
. $lati);
}
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($realtarget)) {
echo "Sorry, there was an error";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["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["fileToUpload"]["tmp_name"], $realtarget))
{
echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has
been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
Der eigentliche Teil, wo die Datei hochgeladen ist die gleiche wie auf den w3 Tutorials verwendet. Ich habe etwas Code hinzugefügt, um einen Platzordner zu erstellen, wenn es vorher keinen Platzordner für diesen Ort gibt, und füge die Koordinaten des Platzes in einer txt-Datei hinzu, die Koordinaten genannt wird.
Die seltsame Sache ist, dass manchmal die Datei in das übergeordnete Verzeichnis hochgeladen wird und der Ort-Ordner nicht erstellt wird. In anderen Fällen wird der Ordner "Ort" erstellt und die Bilddatei wird überhaupt nicht hochgeladen.
Also meine Frage ist, was hier vor sich geht. Ich verstehe nicht, warum es manchmal eine Sache macht und andere nicht mit genau dem gleichen Bild.
<form action="upload.php" id="uploadForm" class="" method="post" enctype="multipart/form-data" style="width:100%; height:100%; font-size:400px">
<input type="file" class="inputfile" name="fileToUpload" id="fileToUpload">
<label for="fileToUpload" style="margin-left:35%; margin-top:8%; width:30%; height:16%; text-align:center"> <img style="width:70%; margin-top:6%"src="img/image.svg"></img></label>
<input id="searchTextField" type="text" style="width:32%; height:50px; margin-left:33%; font-size:22pt; margin-top:30%" name="place">
<script>
function initialize() {
var options = {
types: ['(cities)']
};
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<input style="width:30%; height:8% ; font-size:23pt; margin-top:40%;
margin-left:35%" type="button" class="btn btn-success "
onclick="submitFirst();" id="connectWorld" value="Connect the world!"
name="submit">
<input id="submitbutton" type="submit" style="display:none"></input>
</form>
Hat jemand eine Idee von was ist das Problem hier?
Der Code, den Sie kopieren/einfügen, sieht nicht wie gültiges PHP aus ... Oder ist es nur die echte schlechte Einrückung? – Salketer
es funktioniert manchmal so ich denke, es ist nur hässlich Codierung von mir @Salketer –
Ich habe die Einrückung neu gemacht, scheint alles gut zu mir ... Nur was ich sehe, ist, wenn $ _POST ["Ort"]; ist leer, dann wird es direkt hochgeladen ../place/ – Salketer