Search This Blog
Sunday, December 15, 2013
HTML5 drag & drop API
Location:
Rajkot, Gujarat, India
HTML5 File and Folder Upload Control
Location:
Rajkot, Gujarat, India
Multiple File Upload using HTML5
Location:
Rajkot, Gujarat, India
Ajax Uploading With HTML5 File api
Labels:
API,
HTML5,
JavaScript,
JQuery
Location:
Rajkot, Gujarat, India
Friday, December 13, 2013
How to Build Shopping Cart w/ Checkout in PHP
Download Script
I just rewrite the code included in this post to fix minor problem encounter by other PHP programmers who can’t run the shopping cart.
The code is taken from this link: http://www.qualitycodes.com/tutorial.php?articleid=25&title=Tutorial-Building-a-shopping-cart-in-PHP.
As you may have noticed, a lot of programmers are screaming for help on how to fix some bugs on this tutorial. Since I cannot upload the code in the said website, I decided to post it here. Still credit goes to “Richard Clark”.
Changes on the code are following:
- Change “<?” to “<?php” to handle php correctly. Some old program still uses “<?” to run PHP code. However, PHP 5 is not stricter and requires you to use php after <?, unless you configure Apache to allow such a shorthand support.
- Add echo command instead of equal (=) sign.
Note: If you run the code prior to downloading this code, make sure to clear your browser cache.
Create a Zip File Using PHP
Creating .ZIP archives using PHP can be just as simple as creating them on your desktop. PHP's ZIP class provides all the functionality you need! To make the process a bit faster for you, I've coded a simple create_zip function for you to use on your projects.
The PHP
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
Sample Usage
$files_to_zip = array(
'preload-images/1.jpg',
'preload-images/2.jpg',
'preload-images/5.jpg',
'kwicks/ringo.gif',
'rod.jpg',
'reddit.gif'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');
The function accepts an array of files, the name of the destination files, and whether or not you'd like the destination file to be overwritten if a file of the same name exists. The function returns true if the file was created, false if the process runs into any problems.
Creating ZIP File with PHP
This post helps you to create a ZIP file using PHP, Arun had coded a few lines of script that system converts the selected files into ZIP file format. It is useful for ecommerce web projects like selling PDFs, Images and Docs ect, use can choose files and download it into compressed format. Take a look at this live demo.
Download Script
Live Demo
HTML Code
Form contains list of files with input type checkbox name files[].
PHP Code
Contains PHP code covert the selected files into ZIP file format.
Form contains list of files with input type checkbox name files[].
<form name="zips" method="post">
<input type="checkbox" name="files[]" value="flowers.jpg" />
<img src="files/image.png" />
flowers.jpg
<input type="checkbox" name="files[]" value="fun.jpg" />
<img src="files/image.png" />
fun.jpg
<input type="checkbox" name="files[]" value="9lessons.docx" />
<img src="files/doc.png" />
9lessons.docx
........
<input type="submit" name="createpdf" value="Download as ZIP" />
<input type="reset" name="reset" value="Reset" />
</form>
<input type="checkbox" name="files[]" value="flowers.jpg" />
<img src="files/image.png" />
flowers.jpg
<input type="checkbox" name="files[]" value="fun.jpg" />
<img src="files/image.png" />
fun.jpg
<input type="checkbox" name="files[]" value="9lessons.docx" />
<img src="files/doc.png" />
9lessons.docx
........
<input type="submit" name="createpdf" value="Download as ZIP" />
<input type="reset" name="reset" value="Reset" />
</form>
PHP Code
Contains PHP code covert the selected files into ZIP file format.
<?php
$error = ""; //error holder
if(isset($_POST['createpdf']))
{
$post = $_POST;
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip'))
{
// Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0)
{
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($post['files'] as $file)
{
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
$error .= "* Please select file to zip ";
}
else
$error .= "* You dont have ZIP extension";
}
?>
$error = ""; //error holder
if(isset($_POST['createpdf']))
{
$post = $_POST;
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip'))
{
// Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0)
{
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($post['files'] as $file)
{
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
$error .= "* Please select file to zip ";
}
else
$error .= "* You dont have ZIP extension";
}
?>
Subscribe to:
Posts (Atom)