Hi there! im new to php , im having problem when upload image to server. Hope somebody can help me.
i upload picture in admin CMS, the pic able to save in mysql but is not save in my server, the script below :
$catImage = uploadImage('fleImage', SRV_ROOT . 'images/category/');
i have check the folder/directory name all correct but the picture wont save in my server images folder & i try this script but not working as well:
$target_path = "images/category/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
i got this tutorial from :
PHP MySQL Shopping Cart Tutorial
Thank You In Advance
Please explain to me Stalion what ma not doing right. But it works for me
What you pasted is not a function definition. Look through (or Search In Files, if your editor supports it) the code until you find the words “function uploadImage”
okay
<?php
require_once '../../library/config.php';
require_once '../library/functions.php';
checkUser();
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'add' :
addCategory();
break;
case 'modify' :
modifyCategory();
break;
case 'delete' :
deleteCategory();
break;
case 'deleteImage' :
deleteImage();
break;
default :
// if action is not defined or unknown
// move to main category page
header('Location: index.php');
}
/*
Add a category
*/
function addCategory()
{
$name = $_POST['txtName'];
$description = $_POST['mtxDescription'];
$image = $_FILES['fleImage'];
$parentId = $_POST['hidParentId'];
$catImage = uploadImage('fleImage', SRV_ROOT . 'images/category/');
$sql = "INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description, cat_image)
VALUES ($parentId, '$name', '$description', '$catImage')";
$result = dbQuery($sql) or die('Cannot add category' . mysql_error());
header('Location: index.php?catId=' . $parentId);
}
/*
Upload an image and return the uploaded image name
*/
function uploadImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
// if a file is given
if (trim($image['tmp_name']) != '') {
// get the image extension
$ext = substr(strrchr($image['name'], "."), 1);
// generate a random new file name to avoid name conflict
////////////$imagePath = md5(rand() * time()) . ".$ext";///////////////// remove md5 function ///////
$imagePath = $image['name'] ;
// check the image width. if it exceed the maximum
// width we must resize it
$size = getimagesize($image['tmp_name']);
if ($size[0] > MAX_CATEGORY_IMAGE_WIDTH) {
$imagePath = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_CATEGORY_IMAGE_WIDTH);
} else {
// move the image to category image directory
// if fail set $imagePath to empty string
if (!move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath)) {
$imagePath = '';
}
}
}
return $imagePath;
}
/*
Modify a category
*/
function modifyCategory()
{
$catId = (int)$_GET['catId'];
$name = $_POST['txtName'];
$description = $_POST['mtxDescription'];
$image = $_FILES['fleImage'];
$catImage = uploadImage('fleImage', SRV_ROOT . 'images/category/');
// if uploading a new image
// remove old image
if ($catImage != '') {
_deleteImage($catId);
$catImage = "'$catImage'";
} else {
// leave the category image as it was
$catImage = 'cat_image';
}
$sql = "UPDATE tbl_category
SET cat_name = '$name', cat_description = '$description', cat_image = $catImage
WHERE cat_id = $catId";
$result = dbQuery($sql) or die('Cannot update category. ' . mysql_error());
header('Location: index.php');
}
/*
Remove a category
*/
function deleteCategory()
{
if (isset($_GET['catId']) && (int)$_GET['catId'] > 0) {
$catId = (int)$_GET['catId'];
} else {
header('Location: index.php');
}
// find all the children categories
$children = getChildren($catId);
// make an array containing this category and all it's children
$categories = array_merge($children, array($catId));
$numCategory = count($categories);
// remove all product image & thumbnail
// if the product's category is in $categories
$sql = "SELECT pd_id, pd_image, pd_thumbnail
FROM tbl_product
WHERE cat_id IN (" . implode(',', $categories) . ")";
$result = dbQuery($sql);
while ($row = dbFetchAssoc($result)) {
@unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_image']);
@unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_thumbnail']);
}
// delete the products
$sql = "DELETE FROM tbl_product
WHERE cat_id IN (" . implode(',', $categories) . ")";
dbQuery($sql);
// then remove the categories image
_deleteImage($categories);
// finally remove the category from database;
$sql = "DELETE FROM tbl_category
WHERE cat_id IN (" . implode(',', $categories) . ")";
dbQuery($sql);
header('Location: index.php');
}
/*
Recursively find all children of $catId
*/
function getChildren($catId)
{
$sql = "SELECT cat_id ".
"FROM tbl_category ".
"WHERE cat_parent_id = $catId ";
$result = dbQuery($sql);
$cat = array();
if (dbNumRows($result) > 0) {
while ($row = dbFetchRow($result)) {
$cat[] = $row[0];
// call this function again to find the children
$cat = array_merge($cat, getChildren($row[0]));
}
}
return $cat;
}
/*
Remove a category image
*/
function deleteImage()
{
if (isset($_GET['catId']) && (int)$_GET['catId'] > 0) {
$catId = (int)$_GET['catId'];
} else {
header('Location: index.php');
}
_deleteImage($catId);
// update the image name in the database
$sql = "UPDATE tbl_category
SET cat_image = ''
WHERE cat_id = $catId";
dbQuery($sql);
header("Location: index.php?view=modify&catId=$catId");
}
/*
Delete a category image where category = $catId
*/
function _deleteImage($catId)
{
// we will return the status
// whether the image deleted successfully
$deleted = false;
// get the image(s)
$sql = "SELECT cat_image
FROM tbl_category
WHERE cat_id ";
if (is_array($catId)) {
$sql .= " IN (" . implode(',', $catId) . ")";
} else {
$sql .= " = $catId";
}
$result = dbQuery($sql);
if (dbNumRows($result)) {
while ($row = dbFetchAssoc($result)) {
// delete the image file
$deleted = @unlink(SRV_ROOT . CATEGORY_IMAGE_DIR . $row['cat_image']);
}
}
return $deleted;
}
?>
I use this for my website:
$target_path = “images/category/”;
$target_path = $target_path . basename( $_FILES[‘uploadedfile’][‘name’]);
Then i insert to database
//Writes the information to the database
mysql_query(“INSERT INTO banner (uploadedfile) VALUES (‘$uploadedfile’)”);
Then create a function to upload for you
function upload ($file, $path){
move_uploaded_file($file, $path);
}
Then call your function to upload your photo
$picture = $_FILES[‘uploadedfile’][‘tmp_name’];
thumb($picture, $target_path);
If you have already uploaded your script to server, you have to make sure your folder permission is set to 777 using your ftp software. I use filezilla.
Let me know if my tip helps.
Cheers and happy coding:eye:
Can you please post the code for the uploadImage function?
Hi guys ! Thank you so much !! problem solved
My mistakes here:
$catImage = uploadImage(‘fleImage’, SRV_ROOT . ‘images/category/’);
I change it to:
$catImage = uploadImage(‘fleImage’, ‘…/…/images/category/’);
On my config.php:
define(‘SRV_ROOT’, ‘/root/www/plaincart/’);
shedishedi: I had tried the method you gave but is not working for me
but Thank you so much for helping appreciate it
StarLion: the only words “function uploadImage” i found is only in the processCategory.php file that i provide, i had tried looking for the words but no luck. anyways…Thanks for helping