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

  • Home
  • SEARCH
  • 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 146851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:40:37+00:00 2026-05-11T08:40:37+00:00

how can I prevent file_get_contents from creating an empty file when being used as

  • 0

how can I prevent file_get_contents from creating an empty file when being used as a test condition in an if clause?

An empty file is created regardless, which causes a subsequent call in a different method to getimagesize() to fail.

The problem is, that as I have my code setup, the first time it is called will determine to save an image or to display a previously saved imaged. This is in part dependent on the presence of a file. As an empty file is created, this causes problems when calling my code subsequent times.

Is the easiest way to add a check if the file exists and is greater than 0?

Regardless of if my code works, file_get_contents will still output an error. This error is accounted for and dealt with(by my if condition), so I would like to avoid the error interrupting the output of my application if possible. Is there a way to turn this off without hiding actual errors?

if (file_put_contents($imageDir . $pk . '.jpg', file_get_contents($pic_url)))  {         return $imageDir . $pk . '.jpg';  }  else  {         return 'removed.jpg'; } 
  • 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. 2026-05-11T08:40:38+00:00Added an answer on May 11, 2026 at 8:40 am

    It isn’t file_get_contents() that is creating an empty file, it is file_put_contents().

    file_put_contents() will create a file even if the second parameter is empty. Hence, empty file.

    You’ll need to check the file exists first.

    The easiest fix would be to move file_put_contents() inside the conditional, so that it only creates a file if there are contents.

    if (($filecontents = file_get_contents($pic_url)) !== false)  {     file_put_contents($imageDir . $pk . '.jpg', $filecontents);     return $imageDir . $pk . '.jpg';  }  else  {     return 'removed.jpg'; } 

    Now, this still leaves you with a bunch of problems.

    • Unless you are validating the $pic_url properly, you are leaving yourself open for security vulnerabilities. What if the user inputs a relative path to a local file?
    • file_get_contents() will throw a warning if the file cannot be found. Normally, you would deal with this by checking file_exists() first, but this is NOT possible here, because the http: wrapper does not support file_exists(). Therefore you can supress the error with @ before file_get_contents(). Suppressing errors like this should be avoided in most cases.
    • Even if you suppress the error with ‘@’, the call to file_get_contents() may still take some time – if the address is wrong, it may result in getting no reply from a server, which will cause it to hit a timeout (probably 30 seconds) during which time your script does not run and therefore an end user can get no feedback. This should be taken into account in your app.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 127k
  • Answers 127k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should pass char* to ifstream constructor, use c_str() function.… May 12, 2026 at 5:27 am
  • Editorial Team
    Editorial Team added an answer That overload has been removed in v2.1. May 12, 2026 at 5:27 am
  • Editorial Team
    Editorial Team added an answer I discovered this problem. It's a weird behavior in Delphi7… May 12, 2026 at 5:27 am

Related Questions

I have a Java process that opens a file using a FileReader. How can
Alright so here is my issue. I'm working a game engine that will eventually
The problem is, that my XSLT-Transformation process ( called by .NET ), doesn't leave
--Edit with more bgnd information-- A (black box) COM object returns me a string.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.