title: Detecting if a file exists in wordpress theme?
body: <p>I am creating a wordpress template.</p>
  
  <p>I am displaying one out of a directory of videos, as in</p>
  
  <pre><code>&lt;video controls preload="auto" autoplay loop poster="&lt;?= item_image; ?&gt;"&gt;
  &lt;source src="&lt;?= $item_video_mp4; ?&gt;" type='video/mp4' /&gt;
  &lt;source src="&lt;?= $item_video_webm; ?&gt;" type='video/webm' /&gt;
  &lt;/video&gt;
  </code></pre>
  
  <p>However, in some cases, there is no video file for the item.  In that case, I'd just like to display alternate content.</p>
  
  <p>I was trying to use php's <code>file_exists()</code> but I read that it only works with local paths, not urls.  So I was trying to use a local relative path to to files.</p>
  
  <p>My url to the videos is calculated from the post title:</p>
  
  <pre><code>$item_path = '/wp-content/uploads/item_media/';
  $item_name = get_the_title();
  $item_video_mp4 =  $item_path . $item_name .'.mp4';
  </code></pre>
  
  <p>This works, it displays the video and poster frame if they exist.  The problem is, I need to detect if the video does not exist.</p>
  
  <p>Since the php template is in <code>/wp-content/themes/mytheme</code> as typical I was trying to use</p>
  
  <pre><code>$filepath = '../../uploads/item_media/'. $item_name .'.mp4';
  if (file_exists($filepath)) {
     echo "The file $filepath exists";
  } else {
     echo "The file $filepath does not exist";
  }
  </code></pre>
  
  <p>But the text always returns false.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have also tried this for the path:</p>
  
  <pre><code>$filepath = WP_CONTENT_URL . '/uploads/item_media/'. $item_name .'.mp4'
  </code></pre>
  
  <p>Which echoes  </p>
  
  <pre><code>"http://des.dev/wp-content/uploads/item_media/4595DW.mp4 does not exist"
  </code></pre>
  
  <p>Even though it does.</p>