私はHTML形式から画像をアップロードするPHPスクリプトを用意しています。 PHPファイルは次のようになります。W3チュートリアルで使用されるようにPHPで画像をアップロードすることができません
<?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.";
}
}
}
?>
ファイルがアップロードされ、実際の部分は同じです。私は以前からその場所のための場所フォルダがない場合、場所フォルダを作るためにいくつかのコードを追加し、座標と呼ばれるtxtファイルに場所の座標を追加します。
奇妙なことは、ファイルが親ディレクトリにアップロードされ、場所フォルダが作成されないことがあるということです。他の場所では、場所のフォルダが作成され、画像ファイルはまったくアップロードされません。
私の質問はここで何が起こっているのですか。私はなぜそれが時々1つの事をし、他の時がまったく同じ絵でいけないのか分からない。
<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>
誰でも問題はここにあるかのアイデアを持っていますか?
コピー/ペーストしたコードが有効なPHPのように見えません...それとも本当に悪い字下げですか? – Salketer
それはときどき動作するので、私はちょうど醜いコーディングをしていると思います@Salketer –
私はインデントを作り直しました。私にはうってつけです... $ _POST ["place"];空の場合は、直接../place/ – Salketer