Write JSON to file in PHP

$json = ‘{"key":"value"}’; $file = new File(’/path/to/file’, true); $file->write($json);
Continue reading…

Enjoyed this post? Share it!

 
 

Secure image upload script in PHP

<?php // upload.php echo <<<_END <html><head><title>PHP Form Upload</title></head><body> <form method=’post’ action=’upload2.php’ enctype=’multipart/form-data’> Select a JPG, GIF, PNG or TIF File: <input type=’file’ name=’filename’ size=’10’ /> <input type=’submit’ value=’Upload’ /></form> _END; if ($_FILES) { $name = $_FILES[’filename’][’name’]; switch($_FILES[’filename’][’type’]) { case ‘image/jpeg’: $ext = ‘jpg’; break; case ‘image/gif’: $ext = ‘gif’; break; case ‘image/png’: $ext = ‘png’; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Contents of the $_FILES array in PHP

$_FILES[‘file’][‘name’] The name of the uploaded file (e.g., somefile.jpg) $_FILES[‘file’][‘type’] The content type of the file (e.g., image/jpeg) $_FILES[‘file’][‘size’] The file’s size in bytes $_FILES[‘file’][‘tmp_name’] The name of the temporary file stored on the server $_FILES[‘file’][‘error’] The error code resulting from the file upload
Continue reading…

Enjoyed this post? Share it!

 
 

Image uploader in PHP

<?php // upload.php echo <<<_END <html><head><title>PHP Form Upload</title></head><body> <form method=’post’ action=’upload.php’ enctype=’multipart/form-data’> Select File: <input type=’file’ name=’filename’ size=’10’ /> <input type=’submit’ value=’Upload’ /> </form> _END; if ($_FILES) { $name = $_FILES[’filename’][’name’]; move_uploaded_file($_FILES[’filename’][’tmp_name’], $name); echo "Uploaded image ‘$name'<br /><img src=’$name’ />"; } echo "</body></html>"; ?>
Continue reading…

Enjoyed this post? Share it!

 
 

How to upload multiple files using PHP and HTTP Post

We can implement this in two ways. One as an array of files and the other as individual variables. Method 1: In the html form, set multiple file input boxes, use the array name as their names, as follows: <form action="upload" method=post> <input type=file name=upfile[]/> <input type=file name=upfile[]/> <input type=file name=upfile[]/> </ Form> On the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Display subfolders from directory using php

Example to display subfolders from directory using php: <?php echo "<h2>subdirs in dir</h2><ul>"; $basedir = basename( __FILE__ ); $dirtoscan = ($basedir . ‘/somedir/’); $albumlisting = scandir($dirtoscan); foreach ($albumlisting as $item) { $dirinfo = pathinfo($item); print_r($dirinfo); if (is_dir("$item")) { echo "<li><a href=’index.php?subdirs=$item’>$item</a></li>"; } } ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Display dropdown file list in PHP using DirectoryIterator

To iterate over all files in a directory, we can use a DirectoryIterator to get each file in the directory. The example shows how to create a <select/> box in a form that lists all the files in a directory. <?php echo "<select name=’file’>\n"; foreach (new DirectoryIterator(’c:\wamp\www’) as $file) { echo ‘<option>’ . htmlentities($file) . […]
Continue reading…

Enjoyed this post? Share it!