Note that there are some explanatory texts on larger screens.

plurals
  1. POsearch a php string for two substrings
    primarykey
    data
    text
    <p>I am programming a php program that will allow users to download audio from my website.<br/> To do this they go to <i>www.mysite.com/downloadit.php?file=myfile.mp3</i> and the download of <i>myfile.mp3</i> will begin immediately. <br/><br/>There is a problem though: I don't want people to be able to download system files. I am going to solve this by checking if <code>$_GET['file']</code> contains the substrings <code>.mp3</code> or <code>.wav</code>. I am trying to do this with the <code>strpos</code> command, but can't get it working. How would I check for more than one substring (.mp3 and .wav) in a string with <code>strpos</code>? Or maybe I should use a different command? Please let me know! <br/> Here is my code so far:<br/></p> <pre><code>$haystack=$_GET['file']; $resulting = strpos($haystack, ".mp3"); //if found $resulting will equal 1 //if not found $resulting will equal 0 //if $resulting is less than one then it must be 0 if($resulting &lt; 1){ //security breach! die("Unauthorized"); } //assuming we passed the last test, everything went well, we will then continue else{ //code here } </code></pre> <p>Thanks to @DoubleSharp i now have this completed code!!! <pre><code>//if it is found $resulting will not equal 0 //if it is not found $resulting will equal 0 //echo the result //echo $resulting; //add a line break echo "&lt;br/&gt;"; //if $resulting is less than one then it must be 0 //since it is 0 it must mean that it is a breach! if (preg_match("~\.(mp3|wav)$~i", $haystack)) { echo "hi nice file"; } else { die("bad file"); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload