Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8407855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:28:58+00:00 2026-06-09T23:28:58+00:00

Ok so I am working on a backoffice at work. This is my first

  • 0

Ok so I am working on a backoffice at work.
This is my first experience on PHP so my code will most likely not be that great.

Here is what I am working on:

The client must be able to upload a image + title + description of a product, it must that show that product on the main page.
After that he must be able to delete or modify that item.

So the upload is working, I managed to show the product, delete it, and modify it.

The only problem is: When I modify with a new image it doesn’t replace the old one in the directory (images take on the id of the sql input, example 0.jpg -> 1.jpg -2.jpg etc.)

So I will show here my upload script and modify :

Upload:

<?php
     include("config.php");

$dir = "../images/";
$dir = $dir . basename( $_FILES['image']['name']);
$title = $_POST['title'];
$description = $_POST['desc'];
$position = $_POST['position'];
$image = ($_FILES['image']['name']);
$uploadedfile = $_FILES['image']['tmp_name'];
$uploadedfiletype = $_FILES['image']['type']; 

if (!($uploadedfiletype =="image/pjpeg" OR $uploadedfiletype =="image/jpeg" OR $uploadedfiletype =="image/jpg")){
      echo "Image must be jpg file";
}else if (move_uploaded_file($_FILES['image']['tmp_name'], $dir)){
    $sql="INSERT INTO test (title, description, position) VALUES ('$title','$description','$position')";
    if (!mysql_query($sql,$con)){
         die('Error: ' . mysql_error());
  }
  $newname = "../images/" . mysql_insert_id() . ".jpg";
  rename ("../images/$image","$newname");    
  $orig_image = imagecreatefromjpeg("../image/$newname");
  $sm_image = imagecreatetruecolor(96,96);
  imagecopyresampled($sm_image,$orig_image,0,0,0,0,96,96,imagesx($orig_image),imagesy($orig_image));
  imagejpeg($sm_image, $newname, 100);
  echo 'Upload good <a href="connected.php">Retour</a>';
}else{
    echo "Problem";
}
?>

And here is my modify script (basicly the same thing but with UPDATE for sql):

<?php

    include("config.php");

$dir = "../images/";
$dir = $dir . basename( $_FILES['image']['name']);
$title = $_POST['title'];
$description = $_POST['desc'];
$position = $_POST['position'];
    $id=$_POST['id'];
$image = ($_FILES['image']['name']);
$uploadedfile = $_FILES['image']['tmp_name'];
$uploadedfiletype = $_FILES['image']['type']; 

unlink('../images/$id');

if (!($uploadedfiletype =="image/pjpeg" OR $uploadedfiletype =="image/jpeg" OR $uploadedfiletype =="image/jpg")){
      echo "L'image doit être en .jpg ou .jpeg";
}else if (move_uploaded_file($_FILES['image']['tmp_name'], $dir)){ 
  $sql="UPDATE test SET title='$title', description='$description' WHERE id='$id'";
    if (!mysql_query($sql)){
         die('Error: ' . mysql_error());
  }
  $newname = "../images/" . mysql_insert_id() . ".jpg";
  rename ("../images/$image","$newname");    
  $orig_image = imagecreatefromjpeg("../image/$newname");
  $sm_image = imagecreatetruecolor(96,96);
  imagecopyresampled($sm_image,$orig_image,0,0,0,0,96,96,imagesx($orig_image),imagesy($orig_image));
  imagejpeg($sm_image, $newname, 100);
  echo 'Modif réussi <a href="connected.php">Retour</a>';
}else{
    echo "Un probleme est survenue";
}

 ?>

Thanks in advance for any help !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:28:59+00:00Added an answer on June 9, 2026 at 11:28 pm

    It seems you got a typo error near unlink('../images/$id'); I mean, PHP doesn’t compile $id within the single quotes.

    Try doing unlink("../images/$id"); or unlink('../images/'.$id);

    If it still doesn’t work, You might have a right issue. Try debugging it by doing var_dump(unlink("../images/$id")) if it returns false, it means deletion failed.

    If so, try creating a new file, using a sha1 hash for example:

    $dir = "../images/".sha1(rand())."-".basename( $_FILES['image']['name']);

    This will create a new unique name for each uploaded file. It will store the old images, but under different name.
    Just call them differently into your database

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with Json, how can I NSlog only the title in this code: NSDictionary
Working through this for fun: http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/ Example calculation of nullable and first uses a
Working on an html5 app for a touchscreen windows xp computer that will be
Working with an undisclosed API, I found a function that can set the number
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
Working with H2 I get this error when I try to write a row
Working with MS Access for the first time and coming across a few problems
Working on game where plates will be falling from top to bottom. Some plates
I'm working on a site that's grown both in terms of user-base and functionality
Working on an extension that use the new experimental devtools apis. How do you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.