Save image file on disk from image data in sql table
<p>I have an sql table that stores images in varbinary format. I want to take the image data from sql table and store it as image file on my system.</p>

<pre><code>$getPhotos = "SELECT Name, PhotoContent
              FROM [Photos]";                 
$resultPhotos = odbc_exec($con, $getPhotos);

while($post = odbc_fetch_array($resultPhotos)){
    $fileData = $post['PhotoContent'];
    $fileName = "D:\\Images\\".$post['Name'];
    file_put_contents($fileName,str_replace('data:image/jpeg;base64,','',$fileData));
}
</code></pre>

<p>The above code saves the image file on the system but, the saved image is broken.</p>

<p><a href="https://i.stack.imgur.com/8NnFp.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8NnFp.jpg" alt="enter image description here"></a></p>

<p>How do I fix this issue ? </p>

<p>Thanks</p>