We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6024
    • 17 Posts
    質問ばっかしてるcrouzです。
    今回は素人の私がphpコードをスニペットとして登録し使うにはどうしたら良いか?
    というプログラマな方々には初歩的すぎる質問です。スイマセンです。

    以下のファイルアップロード用php(一応動きます)を使おうと思ってそれらしく書き換えたりして
    スニペットに登録したのですが全然まともに動きません。
    どなたかアドバイスか参考ドキュメントのURLなど教えて下さいまし。
    <?php
    	if ($_POST['del_name']) {
    		if (is_file("./upload/" . $_POST['del_name'])) {
    			unlink(	"./upload/" . $_POST['del_name']);
    			unlink("./desc/" . $_POST['del_name'] . ".txt");
    		}
    	}
    	if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
    		if (!is_file("./upload/" . basename($_FILES['uploadfile']['name']))) {
    			$moveto = "./upload/" . basename($_FILES['uploadfile']['name']);
    			move_uploaded_file($_FILES['uploadfile']['tmp_name'],$moveto) or die("Upload failed.\n");
    			$write_handle = fopen("./desc/" . basename($_FILES['uploadfile']['name']) . ".txt","w+");
    			flock($write_handle,2);
    			fwrite($write_handle,$_POST['description']);
    			flock($write_handle,3);
    			fclose($write_handle);
    			chmod("./desc/" . basename($_FILES['uploadfile']['name']) . ".txt",0646);
    			reload;
    		} else {
    			print("<strong>The same file already exists.\n</strong><br />");
    		}
    	}	
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
    <title>Uploader</title>
    </head>
    <body>
    	<form method="post" action="index.php" enctype="multipart/form-data">
    	Upload File : <input type="file" name="uploadfile"><br />
    	Comment : 
    	<input name="description" type="text" size="80">
    	<br>
    	<input type="submit" name="upload" value="upload">
    	</form>
    	<br>
    
    <form action="index.php" method="post">
    <table width="500">
    	<tr>
    		<th>Files</th>
    		<th>Comments</th>
    		<th>Date</th>
    		<th width="50">Delete</th>
    	</tr>
    <?php
    $dir_handle = opendir("./upload");
    $date = date("Y/m/d", filemtime("./upload/$entry"));
    while ($entry = readdir($dir_handle)) {
    	if($entry != "." && $entry != "..") {
    		$desc_file = file("./desc/" . $entry . ".txt");
    print <<< HTML
    	<tr>
    		<td><a href="./upload/$entry">$entry</a></td>
    		<td>$desc_file[0]</td>
    		<td>$date</td>
    		<td><input type="hidden" name="del_name" value="$entry"><input type="submit" value="Delete"></td>
    	</tr>
    HTML;
    	}
    }
    ?>
    </table>
    </form>
    </body>
    </html>
    


    スニペットにするのは素人では難しいって事ならスッパリ諦めてFileUploadFileDownloadの組み合わせで
    頑張ります cool
    ただ上記の組み合わせではファイル説明がUpload時に付けられないのが・・・。
      • 8382
      • 253 Posts
      crouzさん。
      スニペットの作り方なんてのはなさそうですね :’( 釈迦に説法かもしれませんが、作り方のアドバイスとしましては、通常のphpスクリプトと逆転の発想をしてみてください。スニペットはPHPのサブルーチンみたいなもので、返り値に必要なHTMLを全て詰め込んだ文字列を返すようにします。
      <html>や</html>を含むかどうかはテンプレートやスニペットを呼び出すドキュメントとの兼ね合いで決まります。
      crouzさんなら、それほどむずかしく考える必要ないのでは wink
      後、$_POSTなどのサニタイジングを忘れずに。ファイル操作系はディレクトリトラバーサルなどにも要注意です cool
        • 6024
        • 17 Posts
        eastbindさん、毎度どうもです。

        ボンヤリとですが判った感じがするので地道に勉強を兼ねて頑張ってみます。 wink
        サブルーチンみたいなって説明で光明がさした感じっす。