Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <a href="http://php.net/file_exists" rel="nofollow noreferrer"><code>file_exists()</code></a> function to check if a file exists:</p> <pre><code>if($_FILES['myprofilepicture']['type'] != 'image/jpeg' &amp;&amp; $_FILES['photo']['type'] != 'image/jpg' &amp;&amp; $_FILES['photo']['type'] != 'image/png') { header("location: wrongfile.php"); } else { $info = pathinfo($_FILES['photo']['name']); $randomfile = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,$length); $target = 'picture/'.$randomfile; if(!file_exists($target)) //if file doesn't exist { $now = time(); move_uploaded_file( $_FILES['myprofilepicture']['tmp_name'], $target); mysql_query("Insert into photos(name,photo,date)values('$myname','$randomfile','$now')")or die('database error occured'); header("location:home.php"); } } </code></pre> <p>The <code>if</code> conditional statement in the above piece of code will check if the file already exists, and if not, execute the statements in the block. However, if you want to repeat the process until a unique file path is found, you can use a loop instead:</p> <pre><code>while(!file_exists($target)) { # code ... } </code></pre> <hr> <p>As a sidenote: you're currently inserting user input directly into your SQL query. This is a very bad practice and it makes your query vulnerable to <a href="https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1">SQL injection</a>. You should stop using the deprecated <code>mysql_*</code> functions and start using PDO or MySQLi.</p>
    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.
    1. VO
      singulars
      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