Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Image overwrite problem!

    Hi

    My situation is too simple, I'm having an image in my page displayed to clients, ok? I also have a form in the same page that accepts an image file to be uploaded. The image that is uploaded via this form should take the place of the image that's displayed in the page. I always use one name for that image: "main.jpg" and when a new image is uploaded, I move it to the same directory and name it as "main.jpg", which means it overwrites the previous image. So, some lines after the PHP code that does this, I'm having my normal HTML img tag that shows the image.

    But the problem is that whenever I upload an image, it still shows the previous image in the page and only if I press F5, it'll change it. I'm sure that the new image overwrites the previous one (I go to the directoy and check it!!), but I dunno why it doesn't show the new one. I think even though my HTML code is after my PHP code, but b4 it excecutes the PHP code, it'll read the image and because of that I still get the old image when I press the Upload button. I really don't know what to do to change the order, any help is greately appriciated.

    bijan

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I believe it's just the browser caching the old image, and as the image name is the same as last time, it knows no different. I solved this problem by using the following:

    PHP Code:
    $img '<img src="imagename.jpg?'.rand(1,9999999999).'">'

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, yes, it's browser caching, but your trick doesn't work for me! I tried it but I still get my old picture. Any help?? PLEASEEEEEE

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    you can do no more than Nightfire suggests , view-source , is the random string actually being added to the imagename ? if so is it random ?

    if you have php <4.2 then seed the random number first (it should still work without but try anyway)

    PHP Code:
    <?php
    srand
    mktime( ) ) ;
    $img '<img src="imagename.jpg?'.rand(1,9999999999).'">';
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •